我是看了韩顺平的视频学习的mysql 我以为讲的挺详细的
连接: http://pan.baidu.com/s/1hsHugRU
若是被屏蔽了,给我留言我给你发 或q 1228901986mysql
Mysql面试
语句
基本语句
1create database test charset utf8;
2 use test //选用数据库
3 showt databases //查看数据库
3 showt tables //查看表
5 insert into stu //表插入数据
6 select * from stu;//选择表
7set names 字符集
primary key 主键
数据
删除数据
delete from [表] where
delete from class where foormany =0;
插入数据
insert into class
(id,sname,gender,company,salary,foormany)
values
(2,"王",'男','百度',9088,30);sql
当插入正行的时候,能够不用谢数据库
修改数据
update class
set foormany = 123
where id=123;编程
update class
set foormany=123 where gender="男" and foormany >0;数组
查询数据服务器
select sname,gender,company from class where id=1 ;
select * form [表]; 选择全部
列
建列cookie
create table stu( sname varchar(20),yy varchar(20)); //建立表函数
create table class(
id int primary key auto_increment,
sname varchar(10) not null default '',
gender char(1) not null default '',
company varchar(20) not null default '',
salary decimal(6,2) not null default 0.00,
foormany smallint not null default 0 {没有逗号!!!!!!!!!!!!!!!!!!!!!!!!!!!}
)engine myisam charset utf8;
create database tieba charset utf8;
use tieba;学习
create table thread(
tid int not null auto_increment primary key,
username varchar(20) not null default '',
title varchar(30) not null default '',
content text,
pubtime int unsigned not null default 0
) engine myisam charset utf8;
create table reply(
rid int not null auto_increment primary key,
tid int not null default 0,
username varchar(20) not null default '',
content text,
reptime int unsigned not null default 0
) engine myisam charset utf8;
增长列
alter table class【table name】 add score【selsect name】 tinyint unsigned not null default 0;
alter table 表名 add列名称 列类型 列参数 after [某列] {加在制定位置}
alter table class add gender tinyint not null default 0 after age;
alter table 表名 add列名称 列类型 列参数 first {加在制定位置}
alter table class add names char(3) not null default 0 first;
删除列
Alter table [kuname] drop [tablename]
修改列类型
Alter table [表名] modify [列名] [新类型] [新参数]
alter table class modify names char(4) not null default 0;
修改列名和列类型
Alter table [表名] change [旧列名][新列名][新类型][新参数]
alter table class change names nam char(9) not null default '';
重命名
create table stu(
sname varchar(10)
)engine myisam charset utf8;
库
清空数据
truncate stu; 清空表
truncate 至关于清空表在建立新的表
删除库
drop database isam2016; //删除数据库
基本概念理解
查询模型
And 的优先级别or的高
把列看作是变量,变量之间能够进行比较的(广义投影)
Shop_price Mark_price Shop_price – mark_price
A B a-b
能够对AB进行数据处理
起别名(Shop_price – mark_price) as discount
转义
防止注入
foreach ($_POST as $k => $v){
if (is_string($v)){
$_POST[$k] = addslashes($v);
}
}
print_r($_POST);
可是没有递归处理
可使用array_walk_recursive 处理递归的函数
在PHP中有一个魔术引号的概念。在PHP中的PHP.init中,magic_quotes_gpc = On;
重启Apache就行
魔术引号开始的时候,$_GET,$POST,$_cookie,会被系统自动的转义
判断魔术引号 if(get_magic_quotes_gpc())
if(!get_magic_quotes_gpc()){
function _abc( &$v, $k ){ $v = addslashes($v); };
array_walk_recursive(&$_POST,'_abc');
array_walk_recursive(&$_GET,'_abc');
array_walk_recursive(&$_COOKIE,'_abc');
}
列类型
建表的过程就是建列的过程存储一样的数据,不一样的列类型所占据的的空间和效率是不同的,因此知道列类型和存储范围和占据的字节关系
Bane name name name
列类型分类
占据一个字节,空间大小是 255(0—28-1)
没有符号的状况下 0 – 28 -1 0-255
有符号的状况下 -27 – 27 -1 -128 – 127
占据两个个字节,空间大小是 65535(0—216-1)
没有符号的状况下 0 – 216 -1 0-65535
有符号的状况下 -215 – 215 -1 -32768 - 32767
Tee
整形(默认状况下是有符号的)
Tinyint
Smallint
Mediumint
Int
Bigint
对于int 类型,数值越大,占据的空间越大
Int系列的声明时参数 (m) Unsigned Zerofill
m
Unsigned
Zerofill m 必须和zerofill 配合才有意义,而且默认是unsigned
好比 学号不为零 相似00001
alter table class add snum smallint(5) zerofill not null default 0;
浮点型
Float (M,D)
decimal(M,D)(精度高)
M叫 “精度” 表明总位数
D叫“标度” 表明小数位
浮点型默认是没有符号的,不能为负数
浮点型的醉却是10^ 38
还有一种类型是定点行
字符类型
Char(n) 定长字符串 0<=n<=255
查找行记录时若是都是定长,彻底能够能够根据行数于行长来计算出来
若是不够制定的字符长度,则自动用空格补全长度,取出的时候在把右侧的空格去掉
Varchar(n) 变长类型 长度是0-655
使用1-2 个字节来标注,本身的长度
二者是: 都是限制的字符,不是字节
Text类型
能够存储比较的文本类型
没必要声明默认类型
Text
Blob 是二进制的类型,用来存储图像,音频等二进制
时间类型
date
time
Datatime
timestamp
建表的原则
频繁使用的东西放到一张的表中其余放到另外一张的表中
mysql复习
一:复习前的准备
1:确认你已安装wamp
2:确认你已安装ecshop,而且ecshop的数据库名为shop
二 基础知识:
1.数据库的链接
mysql -u -p -h
-u 用户名
-p 密码
-h host主机
2:库级知识
2.1 显示数据库: show databases;
2.2 选择数据库: use dbname;
2.3 建立数据库: create database dbname charset utf8;
2.3 删除数据库: drop database dbname;
3: 表级操做:
3.1 显示库下面的表
show tables;
3.2 查看表的结构:
desc tableName;
3.3 查看表的建立过程:
show create table tableName;
3.4 建立表:
create table tbName (
列名称1 列类型 [列参数] [not null default ],
....列2...
....
列名称N 列类型 [列参数] [not null default ]
)engine myisam/innodb charset utf8/gbk
3.4的例子:
create table user (
id int auto_increment,
name varchar(20) not null default '',
age tinyint unsigned not null default 0,
index id (id)
)engine=innodb charset=utf8;
注:innodb是表引擎,也能够是myisam或其余,但最经常使用的是myisam和innodb,
charset 经常使用的有utf8,gbk;
3.5 修改表
3.5.1 修改表之增长列:
alter table tbName
add 列名称1 列类型 [列参数] [not null default ] #(add以后的旧列名以后的语法和建立表时的列声明同样)
3.5.2 修改表之修改列
alter table tbName
change 旧列名 新列名 列类型 [列参数] [not null default ]
(注:旧列名以后的语法和建立表时的列声明同样)
3.5.3 修改表之减小列:
alter table tbName
drop 列名称;
3.5.4 修改表之增长主键
alter table tbName add primary key(主键所在列名);
例:alter table goods add primary key(id)
该例是把主键创建在id列上
3.5.5 修改表之删除主键
alter table tbName drop primary key;
3.5.6 修改表之增长索引
alter table tbName add [unique|fulltext] index 索引名(列名);
3.5.7 修改表之删除索引
alter table tbName drop index 索引名;
3.5.8 清空表的数据
truncate tableName;
4:列类型讲解
列类型:
整型:tinyint (0~255/-128~127) smallint (0~65535/-32768~32767) mediumint int bigint (参考手册11.2)
参数解释:
unsigned 无符号(不能为负) zerofill 0填充 M 填充后的宽度
举例:tinyint unsigned;
tinyint(6) zerofill;
数值型
浮点型:float double
格式:float(M,D) unsigned\zerofill;
字符型
char(m) 定长
varchar(m)变长
text
列 实存字符i 实占空间 利用率
char(M) 0<=i<=M M i/m<=100%
varchar(M) 0<=i<=M i+1,2 i/i+1/2<100%
year YYYY 范围:1901~2155. 可输入值2位和4位(如98,2012)
日期时间类型 date YYYY-MM-DD 如:2010-03-14
time HH:MM:SS 如:19:26:32
datetime YYYY-MM-DD HH:MM:SS 如:2010-03-14 19:26:32
timestamp YYYY-MM-DD HH:MM:SS 特性:不用赋值,该列会为本身赋当前的具体时间
5:增删改查基本操做
5.1 插入数据
insert into 表名(col1,col2,……) values(val1,val2……); -- 插入指定列
insert into 表名 values (,,,,); -- 插入全部列
insert into 表名 values -- 一次插入多行
(val1,val2……),
(val1,val2……),
(val1,val2……);
5.3修改数据
update tablename
set
col1=newval1,
col2=newval2,
...
...
colN=newvalN
where 条件;
5.4,删除数据 delete from tablenaeme where 条件;
5.5, select 查询
(1) 条件查询 where
a. 条件表达式的意义,表达式为真,则该行取出
b. 比较运算符 = ,!=,< > <= >=
c. like , not like ('%'匹配任意多个字符,'_'匹配任意单个字符)
in , not in , between and
d. is null , is not null
注意:where发挥做用的时候是没有提供discount 对于结果的列在having
(2) 分组 group by
通常要配合5个聚合函数使用:max,min,sum,avg,count
(3) 筛选 having
(4) 排序 order by
(5) 限制 limit
6: 链接查询
6.1, 左链接
.. left join .. on
table A left join table B on tableA.col1 = tableB.col2 ;
例句:
select 列名 from table A left join table B on tableA.col1 = tableB.col2
左右链接都是以在左边的表的数据为准,沿着左表查右表.
内链接是以两张表都有的共同部分数据为准,也就是左右链接的数据之交集.
7 子查询
where 型子查询:内层sql的返回值在where后做为条件表达式的一部分
例句: select * from tableA where colA = (select colB from tableB where ...);
from 型子查询:内层sql查询结果,做为一张表,供外层的sql语句再次查询
例句:select * from (select * from ...) as tableName where ....
8: 字符集
客服端sql编码 character_set_client
服务器转化后的sql编码 character_set_connection
服务器返回给客户端的结果集编码 character_set_results
快速把以上3个变量设为相同值: set names 字符集
存储引擎 engine=1\2
1 Myisam 速度快 不支持事务 回滚
2 Innodb 速度慢 支持事务,回滚
①开启事务 start transaction
②运行sql;
③提交,同时生效\回滚 commit\rollback
触发器 trigger
监视地点:表
监视行为:增 删 改
触发时间:after\before
触发事件:增 删 改
建立触发器语法
create trigger tgName
after/before insert/delete/update
on tableName
for each row
sql; -- 触发语句
删除触发器:drop trigger tgName;
索引
提升查询速度,可是下降了增删改的速度,因此使用索引时,要综合考虑.
索引不是越多越好,通常咱们在常出现于条件表达式中的列加索引.
值越分散的列,索引的效果越好
索引类型
primary key主键索引
index 普通索引
unique index 惟一性索引
fulltext index 全文索引
综合练习:
链接上数据库服务器
建立一个gbk编码的数据库
创建商品表和栏目表,字段以下:
商品表:goods
goods_id --主键,
goods_name -- 商品名称
cat_id -- 栏目id
brand_id -- 品牌id
goods_sn -- 货号
goods_number -- 库存量
shop_price -- 价格
goods_desc --商品详细描述
栏目表:category
cat_id --主键
cat_name -- 栏目名称
parent_id -- 栏目的父id
建表完成后,做如下操做:
删除goods表的goods_desc 字段,及货号字段
并增长字段:click_count -- 点击量
在goods_name列上加惟一性索引
在shop_price列上加普通索引
在clcik_count列上加普通索引
删除click_count列上的索引
对goods表插入如下数据:
+----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
| goods_id | goods_name | cat_id | brand_id | goods_sn | goods_number | shop_price | click_count |
+----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
| 1 | KD876 | 4 | 8 | ECS000000 | 10 | 1388.00 | 7 |
| 4 | 诺基亚N85原装充电器 | 8 | 1 | ECS000004 | 17 | 58.00 | 0 |
| 3 | 诺基亚原装5800耳机 | 8 | 1 | ECS000002 | 24 | 68.00 | 3 |
| 5 | 索爱原装M2卡读卡器 | 11 | 7 | ECS000005 | 8 | 20.00 | 3 |
| 6 | 胜创KINGMAX内存卡 | 11 | 0 | ECS000006 | 15 | 42.00 | 0 |
| 7 | 诺基亚N85原装立体声耳机HS-82 | 8 | 1 | ECS000007 | 20 | 100.00 | 0 |
| 8 | 飞利浦9@9v | 3 | 4 | ECS000008 | 17 | 399.00 | 9 |
| 9 | 诺基亚E66 | 3 | 1 | ECS000009 | 13 | 2298.00 | 20 |
| 10 | 索爱C702c | 3 | 7 | ECS000010 | 7 | 1328.00 | 11 |
| 11 | 索爱C702c | 3 | 7 | ECS000011 | 1 | 1300.00 | 0 |
| 12 | 摩托罗拉A810 | 3 | 2 | ECS000012 | 8 | 983.00 | 14 |
| 13 | 诺基亚5320 XpressMusic | 3 | 1 | ECS000013 | 8 | 1311.00 | 13 |
| 14 | 诺基亚5800XM | 4 | 1 | ECS000014 | 4 | 2625.00 | 6 |
| 15 | 摩托罗拉A810 | 3 | 2 | ECS000015 | 3 | 788.00 | 8 |
| 16 | 恒基伟业G101 | 2 | 11 | ECS000016 | 0 | 823.33 | 3 |
| 17 | 夏新N7 | 3 | 5 | ECS000017 | 1 | 2300.00 | 2 |
| 18 | 夏新T5 | 4 | 5 | ECS000018 | 1 | 2878.00 | 0 |
| 19 | 三星SGH-F258 | 3 | 6 | ECS000019 | 0 | 858.00 | 7 |
| 20 | 三星BC01 | 3 | 6 | ECS000020 | 13 | 280.00 | 14 |
| 21 | 金立 A30 | 3 | 10 | ECS000021 | 40 | 2000.00 | 4 |
| 22 | 多普达Touch HD | 3 | 3 | ECS000022 | 0 | 5999.00 | 15 |
| 23 | 诺基亚N96 | 5 | 1 | ECS000023 | 8 | 3700.00 | 17 |
| 24 | P806 | 3 | 9 | ECS000024 | 148 | 2000.00 | 36 |
| 25 | 小灵通/固话50元充值卡 | 13 | 0 | ECS000025 | 2 | 48.00 | 0 |
| 26 | 小灵通/固话20元充值卡 | 13 | 0 | ECS000026 | 2 | 19.00 | 0 |
| 27 | 联通100元充值卡 | 15 | 0 | ECS000027 | 2 | 95.00 | 0 |
| 28 | 联通50元充值卡 | 15 | 0 | ECS000028 | 0 | 45.00 | 0 |
| 29 | 移动100元充值卡 | 14 | 0 | ECS000029 | 0 | 90.00 | 0 |
| 30 | 移动20元充值卡 | 14 | 0 | ECS000030 | 9 | 18.00 | 1 |
| 31 | 摩托罗拉E8 | 3 | 2 | ECS000031 | 1 | 1337.00 | 5 |
| 32 | 诺基亚N85 | 3 | 1 | ECS000032 | 1 | 3010.00 | 9 |
+----------+------------------------------+--------+----------+-----------+--------------+------------+-------------+
三 查询知识
查询结果就是表
注:如下查询基于ecshop网站的商品表(ecs_goods)
在练习时能够只取部分列,方便查看.
1: 基础查询 where的练习:
查出知足如下条件的商品
1.1:主键为32的商品
select goods_id,goods_name,shop_price
from ecs_goods
where goods_id=32;
1.2:不属第3栏目的全部商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where cat_id!=3;
1.3:本店价格高于3000元的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where shop_price >3000;
1.4:本店价格低于或等于100元的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price <=100;
1.5:取出第4栏目或第11栏目的商品(不准用or)
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where cat_id in (4,11);
1.6:取出100<=价格<=500的商品(不准用and)
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where shop_price between 100 and 500;
1.7:取出不属于第3栏目且不属于第11栏目的商品(and,或not in分别实现)
select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3 and cat_id!=11;
select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id not in (3,11);
1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price>100 and shop_price <300 or shop_price >4000 and shop_price <5000;
1.9:取出第3个栏目下面价格<1000或>3000,而且点击量>5的系列商品
select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods where
cat_id=3 and (shop_price <1000 or shop_price>3000) and click_count>5;
1.10:取出第1个栏目下面的商品(注意:1栏目下面没商品,但其子栏目下有)
select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods
where cat_id in (2,3,4,5);
1.11:取出名字以"诺基亚"开头的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where goods_name like '诺基亚%';
1.12:取出名字为"诺基亚Nxx"的手机
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where goods_name like '诺基亚N__';
1.13:取出名字不以"诺基亚"开头的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goos
where goods_name not like '诺基亚%';
1.14:取出第3个栏目下面价格在1000到3000之间,而且点击量>5 "诺基亚"开头的系列商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where
cat_id=3 and shop_price>1000 and shop_price <3000 and click_count>5 and goods_name like '诺基亚%';
select goods_id,cat_id,goods_name,shop_price from ecs_goods where
shop_price between 1000 and 3000 and cat_id=3 and click_count>5 and goods_name like '诺基亚%';
一道面试题
有以下表和数组
把num值处于[20,29]之间,改成20
num值处于[30,39]之间的,改成30
mian表
+------+
| num |
+------+
| 3 |
| 12 |
| 15 |
| 25 |
| 23 |
| 29 |
| 34 |
| 37 |
| 32 |
| 45 |
| 48 |
| 52 |
+------+
update goods set num = floor(num/10)*10 where (num >=20 and num <= 29) or (num >=30 and num <=39) ;
练习题:
把good表中商品名为'诺基亚xxxx'的商品,改成'HTCxxxx',
提示:大胆的把列当作变量,参与运算,甚至调用函数来处理 .
substring(),concat()
2 分组查询group:
2.1:查出最贵的商品的价格
select max(shop_price) from ecs_goods;
2.2:查出最大(最新)的商品编号
select max(goods_id) from ecs_goods;
2.3:查出最便宜的商品的价格
select min(shop_price) from ecs_goods;
2.4:查出最旧(最小)的商品编号
select min(goods_id) from ecs_goods;
2.5:查询该店全部商品的库存总量
select sum(goods_number) from ecs_goods;
2.6:查询全部商品的平均价
select avg(shop_price) from ecs_goods;
2.7:查询该店一共有多少种商品
select count(*) from ecs_goods;
2.8:查询每一个栏目下面
最贵商品价格
最低商品价格
商品平均价格
商品库存量
商品种类
提示:(5个聚合函数,sum,avg,max,min,count与group综合运用)
select cat_id,max(shop_price) from ecs_goods group by cat_id;
count()函数里面的参数是列名的的时候,那么会计算有值项的次数。
Sum()函数里面的参数是列名的时候,是计算列名的值的相加,而不是有值项的总数。
对count()行数还要注意:它会计算总行数。无论你是否有值都会列入计算范围。另一点:mysqlisam引擎很容易得到总行数的统计。查询速度变得更快
概括:实际编程中统计总行数是常常用到的。此时使用count(*)多处可见。我不多看到有人使用列名做为参数:count(a)的状况。即便是这样使用,可能其初衷也是想统计行数。只是不知道!这样所形成的细微差别而错误使用了"列名"的形式。
3 having与group综合运用查询:
取出便宜5000 元的表
select goods_id,market_price,(shop_price-market_price) as discount from goods where 1 having discount >5000;
3.1:查询该店的商品比市场价所节省的价格
select goods_id,goods_name,market_price-shop_price as j
from ecs_goods ;
3.2:查询每一个商品所积压的货款(提示:库存单价)
select goods_id,goods_name,goods_numbershop_price from ecs_goods
3.3:查询该店积压的总货款
select sum(goods_number*shop_price) from ecs_goods;
3.4:查询该店每一个栏目下面积压的货款.
select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id;
3.5:查询比市场价省钱200元以上的商品及该商品所省的钱(where和having分别实现)
select goods_id,goods_name,market_price-shop_price as k from ecs_goods
where market_price-shop_price >200;
select goods_id,goods_name,market_price-shop_price as k from ecs_goods
having k >200;
3.6:查询积压货款超过2W元的栏目,以及该栏目积压的货款
select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id
having k>20000
3.7:where-having-group综合练习题
有以下表及数据
+------+---------+-------+
| name | subject | score |
+------+---------+-------+
| 张三 | 数学 | 90 |
| 张三 | 语文 | 50 |
| 张三 | 地理 | 40 |
| 李四 | 语文 | 55 |
| 李四 | 政治 | 45 |
| 王五 | 政治 | 30 |
+------+---------+-------+
要求:查询出2门及2门以上不及格者的平均成绩
mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
+------+---+------------+
| name | k | avg(score) |
+------+---+------------+
| 张三 | 3 | 60.0000 |
| 李四 | 2 | 50.0000 |
+------+---+------------+
2 rows in set (0.00 sec)
mysql> select name,count(score<60) as k,avg(score) from stu group by name;
+------+---+------------+
| name | k | avg(score) |
+------+---+------------+
| 张三 | 3 | 60.0000 |
| 李四 | 2 | 50.0000 |
| 王五 | 1 | 30.0000 |
+------+---+------------+
3 rows in set (0.00 sec)
mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
+------+---+------------+
| name | k | avg(score) |
+------+---+------------+
| 张三 | 3 | 60.0000 |
| 李四 | 2 | 50.0000 |
+------+---+------------+
2 rows in set (0.00 sec)
mysql> insert into stu
-> values
-> ('赵六','A',100),
-> ('赵六','B',99),
-> ('赵六','C',98);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
+------+---+------------+
| name | k | avg(score) |
+------+---+------------+
| 张三 | 3 | 60.0000 |
| 李四 | 2 | 50.0000 |
| 赵六 | 3 | 99.0000 |
+------+---+------------+
3 rows in set (0.00 sec)
mysql> select name,avg(score) from stu group by name;
+------+------------+
| name | avg(score) |
+------+------------+
| 张三 | 60.0000 |
| 李四 | 50.0000 |
| 王五 | 30.0000 |
| 赵六 | 99.0000 |
+------+------------+
4 rows in set (0.00 sec)
mysql> # 看每一个人挂科状况
mysql> select name,score < 60 from stu;
+------+------------+
| name | score < 60 |
+------+------------+
| 张三 | 0 |
| 张三 | 1 |
| 张三 | 1 |
| 李四 | 1 |
| 李四 | 1 |
| 王五 | 1 |
| 赵六 | 0 |
| 赵六 | 0 |
| 赵六 | 0 |
+------+------------+
9 rows in set (0.00 sec)
mysql> #计算每一个人的挂科科目
mysql> select name,sum(score < 60) from stu group by name;
+------+-----------------+
| name | sum(score < 60) |
+------+-----------------+
| 张三 | 2 |
| 李四 | 2 |
| 王五 | 1 |
| 赵六 | 0 |
+------+-----------------+
4 rows in set (0.00 sec)
mysql> select name,sum(score < 60),avg(score) as pj from stu group by name;
+------+-----------------+---------+
| name | sum(score < 60) | pj |
+------+-----------------+---------+
| 张三 | 2 | 60.0000 |
| 李四 | 2 | 50.0000 |
| 王五 | 1 | 30.0000 |
| 赵六 | 0 | 99.0000 |
+------+-----------------+---------+
4 rows in set (0.00 sec)
mysql> select name,sum(score < 60) as gk ,avg(score) as pj from stu group by name having gk >=2;
+------+------+---------+
| name | gk | pj |
+------+------+---------+
| 张三 | 2 | 60.0000 |
| 李四 | 2 | 50.0000 |
+------+------+---------+
2 rows in set (0.00 sec)
4: order by 与 limit查询
Asc 升序排列
Desc 降顺序排列
多字段排序的方法 order by 1desc/asc, 列2desc/asc , 列3 desc.asc
Limit[offset n]
Offset 跳过几行
N 实际取得的个数
4.1:按价格由高到低排序
select goods_id,goods_name,shop_price from ecs_goods order by shop_price desc;
4.2:按发布时间由早到晚排序
select goods_id,goods_name,add_time from ecs_goods order by add_time;
4.3:接栏目由低到高排序,栏目内部按价格由高到低排序
select goods_id,cat_id,goods_name,shop_price from ecs_goods
order by cat_id ,shop_price desc;
4.4:取出价格最高的前三名商品
select goods_id,goods_name,shop_price from ecs_goods order by shop_price desc limit 3;
4.5:取出点击量前三名到前5名的商品
select goods_id,goods_name,click_count from ecs_goods order by click_count desc limit 2,3;
5 链接查询
语法假设 A表在左侧不动,B表在A表的右侧滑动,A表和B表经过一个关系来筛选B表的行
左连接
A left join B on 条件 [left join C on 条件] :条件为真,则B表对应的行取出,取出的也是一个结果集,也能够看作是一张表,设为C,能够对C进行查询(6大条件)
左连接和右连接的区别?????
左右链接是能够互换的A left join B ===》 B left join A
内链接的特色: 内链接是左右链接的交集;
Select boy.,goy. from boy inner(内链接) join girl on boy.othergirl.other;
外连接:是左右链接的并集(在mysql 中不支持)
5.1:取出全部商品的商品名,栏目名,价格
select goods_name,cat_name,shop_price from
ecs_goods left join ecs_category
on ecs_goods.cat_id=ecs_category.cat_id;
5.2:取出第4个栏目下的商品的商品名,栏目名,价格
select goods_name,cat_name,shop_price from
ecs_goods left join ecs_category
on ecs_goods.cat_id=ecs_category.cat_id
where ecs_goods.cat_id = 4;
5.3:取出第4个栏目下的商品的商品名,栏目名,与品牌名
select goods_name,cat_name,brand_name from
ecs_goods left join ecs_category
on ecs_goods.cat_id=ecs_category.cat_id
left join ecs_brand
on ecs_goods.brand_id=ecs_brand.brand_id
where ecs_goods.cat_id = 4;
5.4: 用友面试题
根据给出的表结构按要求写出SQL语句。
Match 赛程表
字段名称 字段类型 描述
matchID int 主键
hostTeamID int 主队的ID
guestTeamID int 客队的ID
matchResult varchar(20) 比赛结果,如(2:0)
matchTime date 比赛开始时间
Team 参赛队伍表
字段名称 字段类型 描述
teamID int 主键
teamName varchar(20) 队伍名称
Match的hostTeamID与guestTeamID都与Team中的teamID关联
查出 2006-6-1 到2006-7-1之间举行的全部比赛,而且用如下形式列出:
拜仁 2:0 不来梅 2006-6-21
错误点:select teamName, hostTeamID, guestTeamID, matchResult,matchTime from (mat left join team on team.teamID = mat.hostTeamID) left join team on team.teamID = mat.guestTeamID;
由于是 m t t 相连形成名字冲突,没法解决,起一个别名就能够了
Select [列名] as 别名
From [表名] as 表
select t1.teamName, hostTeamID,t2.teamName, guestTeamID, matchResult,matchTime from (mat left join team as t1 on t1.teamID = mat.hostTeamID) left join team as t2 on t2.teamID = mat.guestTeamID;
select t1.teamName, t2.teamName, matchResult,matchTime from (mat left join team as t1 on t1.teamID = mat.hostTeamID) left join team as t2 on t2.teamID = mat.guestTeamID
where matchTime between '2006-06-01' and '2006-07-01';
mysql> select * from m;
+-----+------+------+------+------------+
| mid | hid | gid | mres | matime |
+-----+------+------+------+------------+
| 1 | 1 | 2 | 2:0 | 2006-05-21 |
| 2 | 2 | 3 | 1:2 | 2006-06-21 |
| 3 | 3 | 1 | 2:5 | 2006-06-25 |
| 4 | 2 | 1 | 3:2 | 2006-07-21 |
+-----+------+------+------+------------+
4 rows in set (0.00 sec)
mysql> select * from t;
+------+----------+
| tid | tname |
+------+----------+
| 1 | 国安 |
| 2 | 申花 |
| 3 | 传智联队 |
+------+----------+
3 rows in set (0.00 sec)
mysql> select hid,t1.tname as hname ,mres,gid,t2.tname as gname,matime
-> from
-> m left join t as t1
-> on m.hid = t1.tid
-> left join t as t2
-> on m.gid = t2.tid;
+------+----------+------+------+----------+------------+
| hid | hname | mres | gid | gname | matime |
+------+----------+------+------+----------+------------+
| 1 | 国安 | 2:0 | 2 | 申花 | 2006-05-21 |
| 2 | 申花 | 1:2 | 3 | 传智联队 | 2006-06-21 |
| 3 | 传智联队 | 2:5 | 1 | 国安 | 2006-06-25 |
| 2 | 申花 | 3:2 | 1 | 国安 | 2006-07-21 |
+------+----------+------+------+----------+------------+
4 rows in set (0.00 sec)
6 union查询
语法:SqlA union sqlB;
也能够合并结果,也就是说能从2张表查询在union.
取自两张表,经过‘别名’让两个结果集的列一致。
即便列名不同,也能union ,一第一张的表的列名为准
Union 的使用条件 ,字段的数目相同
Union 后的结果集,可否在排序,是能够的
select goods_id,goods_name,cat_id,shop_price from goods where cat_id = 4 union select goods_id,goods_name,cat_id,shop_price from goods where cat_id = 5 order by
shop_price asc;
取出第三个栏目的价格前三高,和第四个价格前两个高的
若是有重复行的数据 union 的 处理状况
会默认去重, union all 取消默认不去重
6.1:把ecs_comment,ecs_feedback两个表中的数据,各取出4列,并把结果集union成一个结果集.
6.2:3期学员碰到的一道面试题
A表:
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+------+
B表:
+------+------+
| id | num |
+------+------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
mysql> # 合并 ,注意all的做用
mysql> select * from ta
-> union all
-> select * from tb;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
要求查询出如下效果:
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 30 |
| d | 30 |
| e | 99 |
+------+----------+
参考答案:
mysql> # sum,group求和
mysql> select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 25 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.00 sec)
7: 子查询:
Where 类型的查询,
指的是把内层查询的结果做为查询的比较条件
若是Where列 =(内层sql),则内层的sql返回的单行单列的值
若是是where 列 in (内层sql) 有不少的值
查出每一个栏目下的最大的id
select goods_id,goods_name from goods
where goods_id in (select max(goods_id) from goods group by cat_id);
from 类型的查询,计内层的sql的查询结果,当成一张临时表,日工外层的sql 再次查询
查询结果就是表
select * from (select goods_id,cat_id,goods_name from goods order by cat_id asc,goods_id desc) as tmp group by cat_id;
exists 型子查询
要求查出有商品的栏目
select cat_id ,cat_id,cat_name from categroy where exists ( select * from goods where goods.cat_id = categroy.cat_id )
7.1:查询出最新一行商品(以商品编号最大为最新,用子查询实现)
select goods_id,goods_name from
ecs_goods where goods_id =(select max(goods_id) from ecs_goods);
7.2:查询出编号为19的商品的栏目名称(用左链接查询和子查询分别)
7.3:用where型子查询把ecs_goods表中的每一个栏目下面最新的商品取出来
select goods_id,goods_name,cat_id from ecs_goods where goods_id in (select max(goods_id) from ecs_goods group by cat_id);
7.4:用from型子查询把ecs_goods表中的每一个栏目下面最新的商品取出来
select * from (select goods_id,cat_id,goods_name from ecs_goods order by goods_id desc) as t group by cat_id;
建立触发器:
CREATE trigger tg2
after insert on ord
for each row
update goods set goods_number=goods_number-new.num where id=new.gid
CREATE trigger tg3
after delete on ord
for each row
update goods set goods_number=good_number+old.num where id=old.gid
CREATE trigger tg4
after update on ord
for each row
update goods set goods_number=goods_number+old.num-new.num where id=old.gid
2012-03-25更新,添加了面试案例
函数
数学函数
Abs(x) j
Bin () 返回二进制(oct 返回八进制,hex 返回16进制)
Ceiling(x)向上取整
Exp(x) 返回e 的x 次方
Floor(x)
Group_content () 他默认是‘,’
Now()时间 0000-00-00 00-00-00
Curtime() 时间 00-00-00
Curdate() 日期0000-00-00
Dayofweek(n) n是一周的第几天
Week(n) n是第几周
md5()加密函数
user() 返回用户和主机 database() 返回数据库的名称