1、mysql查询的五种子句mysql
where(条件查询)、having(筛选)、group by(分组)、order by(排序)、limit(限制结果数)sql
一、where经常使用运算符:ide
比较运算符:函数
> , < ,= , != (< >),>= ,<=spa
in(v1,v2..vn) 3d
between v1 and v2 在v1至v2之间(包含v1,v2)regexp
逻辑运算符:blog
not ( ! ) 逻辑非排序
or ( || ) 逻辑或图片
and ( && ) 逻辑与
where price>=3000 and price <= 5000 or price >=500 and price <=1000
取500-1000或者3000-5000的值
where price not between 3000 and 5000
不在3000与5000之间的值
模糊查询:
like
regexp
通配符:
% 任意字符
_ 单个字符
where goods_name like '诺基亚%'
where goods_name like '诺基亚N__'
二、group by 分组
通常状况下group需与统计函数(聚合函数)一块儿使用才有意义
如,表goods:
select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;
这里取出来的结果中的goods_id,goods_name是错误的!由于shop_price使用了max函数,那么它是取最大的,而语句中使用了group by 分组,而goods_id,goods_name并无使用聚合函数,它只是cat_id下的第一个商品,并不会由于shop_price改变而改变。
mysql中的五种统计函数:
(1)max:求最大值
select max(shop_price) from goods; 这里会取出最大的价格的值,只有值
例:查询每一个栏目下价格最高(最贵)的商品
select * from goods where shop_price in(
select max(shop_price) from goods group by cat_id
); 由于max函数结合group by就能取出分组下的最大值
可是注意,上面不能写成:
select *,max(shop_price) from goods where shop_price in(select max(shop_price) from goods group by cat_id);
由于只会显示出价格为4999的记录,由于:
上句至关于select *,max(shop_price) from goods where shop_price in(4600,2800,588,4300,3300);
查询结果为:
意思是对in中的数据求最大值,并将其全部字段列出来。
而select * from goods where shop_price in(4600,2800,588,4300,3300);查询结果为:
注意上面两句查询的区别。
还有一种方法,查询每一个栏目下最贵的商品:
先对每一个栏目下的商品按栏目升序、价格降序
select cat_id,goods_id,goods_name,goods_price from goods order by cat_id,goods_price desc;
上面的查询结果中每一个栏目的第一行的商品就是最贵的商品,把上面的查询结果理解为一个临时表[存在于内存中]【子查询】,再从临时表中选出每一个栏目最贵的商品
select * from (select cat_id,goods_id,goods_name,goods_price from goods order by cat_id,goods_price desc) as t group by cat_id;
这里使用group by cat_id是由于临时表中每一个栏目的第一个商品就是最贵的商品,而group by前面没有使用聚合函数,因此默认就取每一个分组的第一行数据,这里以cat_id分组。
(2)min:求最小值
(3)sum:求总数和
mysql> select sum(shop_price) from goods;
+-----------------+
| sum(shop_price) |
+-----------------+
| 16383.00 |
+-----------------+
(4)avg:求平均值
mysql> select cat_id,avg(shop_price) from goods group by cat_id;
求每一个栏目的商品平均价格
+--------+-----------------+
| cat_id | avg(shop_price) |
+--------+-----------------+
| 1 | 4499.000000 |
| 2 | 2943.500000 |
| 3 | 549.500000 |
+--------+-----------------+
(5)count:求总行数
mysql> select cat_id,count(*) from goods group by cat_id;
求每一个栏目下商品种类
+--------+----------+
| cat_id | count(*) |
+--------+----------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
+--------+----------+
要把每一个字段名当成变量来理解,它能够进行运算
例:查询本店每一个商品价格比市场价低多少;
select goods_id,goods_name,goods_price-market_price from goods;
例:查询每一个栏目下面积压的货款
select cat_id,sum(goods_price*goods_number) from goods group by cat_id;
能够用as来给计算结果取个别名
mysql> select cat_id,sum(shop_price * cat_id) as hk from goods group by cat_id;
+--------+----------+
| cat_id | hk |
+--------+----------+
| 1 | 8998.00 |
| 2 | 11774.00 |
| 3 | 3297.00 |
+--------+----------+
不只列名能够取别名,表单也能够取别名
三、having 与where 的异同点
where针对表中的列发挥做用,查询数据
having对查询结果中的列发挥做用,筛选数据
例:查询本店商品价格比市场价低多少钱,输出低200元以上的商品
select goods_id,goods_name,market_price - goods_price as s from goods having s>200 ;
这里不能用where由于s是查询结果,而where只能对表中的字段名筛选,若是用where的话则是:
select goods_id,goods_name from goods where market_price - goods_price > 200;
同时使用where与having:
select cat_id,goods_name,market_price - goods_price as s from goods where cat_id = 2 having s > 500;
例:查询积压货款超过2万元的栏目,以及该栏目积压的货款
mysql> select cat_id,sum(goods_price * goods_num) as s from goods group by cat_id having s > 20000;
+--------+----------+
| cat_id | s |
+--------+----------+
| 1 | 57500.00 |
| 2 | 29600.00 |
+--------+----------+
例:查询两门及两门以上科目不及格的学生的平均分
先建表、添加数据
mysql> select * from stuscore;
+----+------+---------+-------+
| id | name | subject | score |
+----+------+---------+-------+
| 1 | 张静 | 语文 | 45 |
| 2 | 张静 | 数学 | 67 |
| 3 | 张静 | 英语 | 87 |
| 4 | 张静 | 物理 | 76 |
| 5 | 张静 | 化学 | 87 |
| 6 | 王壮 | 语文 | 67 |
| 7 | 王壮 | 数学 | 89 |
| 8 | 王壮 | 英语 | 78 |
| 9 | 王壮 | 物理 | 98 |
| 10 | 王壮 | 化学 | 87 |
| 11 | 刘都 | 语文 | 88 |
| 12 | 刘都 | 数学 | 43 |
| 13 | 刘都 | 英语 | 56 |
| 14 | 刘都 | 物理 | 43 |
| 15 | 刘都 | 化学 | 57 |
| 16 | 周灵 | 语文 | 59 |
| 17 | 周灵 | 数学 | 54 |
| 18 | 周灵 | 英语 | 98 |
| 19 | 周灵 | 物理 | 60 |
| 20 | 周灵 | 化学 | 69 |
| 21 | 李歌 | 语文 | 90 |
| 22 | 李歌 | 数学 | 73 |
| 23 | 李歌 | 英语 | 59 |
| 24 | 李歌 | 物理 | 71 |
| 25 | 李歌 | 化学 | 69 |
+----+------+---------+-------+
-- 每一个学生的平均分
mysql> select name,avg(score) as avgScore from stuscore group by name;
+------+----------+
| name | avgScore |
+------+----------+
| 刘都 | 57.4 |
| 周灵 | 68 |
| 张静 | 72.4 |
| 李歌 | 72.4 |
| 王壮 | 83.8 |
+------+----------+
-- 查出全部学生的挂科状况
mysql> select name,score<60 from stuscore;
+------+----------+
| name | score<60 |
+------+----------+
| 张静 | 1 |
| 张静 | 0 |
| 张静 | 0 |
| 张静 | 0 |
| 张静 | 0 |
| 王壮 | 0 |
| 王壮 | 0 |
| 王壮 | 0 |
| 王壮 | 0 |
| 王壮 | 0 |
| 刘都 | 0 |
| 刘都 | 1 |
| 刘都 | 1 |
| 刘都 | 1 |
| 刘都 | 1 |
| 周灵 | 1 |
| 周灵 | 1 |
| 周灵 | 0 |
| 周灵 | 0 |
| 周灵 | 0 |
| 李歌 | 0 |
| 李歌 | 0 |
| 李歌 | 1 |
| 李歌 | 0 |
| 李歌 | 0 |
+------+----------+
-- 查出两门及两门以上不及格的学生
mysql> select name,sum(score<60) as gk from stuscore group by name having gk > 1;
+------+------+
| name | gk |
+------+------+
| 刘都 | 4 |
| 周灵 | 2 |
+------+------+
-- 综合结果
mysql> select name,sum(score<60) as gk,avg(score) as pj from stuscore group by name having gk >1;
+------+------+------+
| name | gk | pj |
+------+------+------+
| 刘都 | 4 | 57.4 |
| 周灵 | 2 | 68 |
+------+------+------+
四、order by
(1)order by price,order by price asc //默认升序排列
(2)order by price desc //降序排列
(3)order by rand() //随机排列,效率不高
例:按栏目号升序排列,每一个栏目下的商品价格降序排列
select * from goods where cat_id !=2 order by cat_id,goods_price desc;
五、limit
limit [offset,] N
offset 偏移量,可选,不写则至关于limit 0,N
N 取出条目
例:取价格第4-6条的商品
select goods_id,goods_name,goods_price from goods order by goods_price desc;
select goods_id,goods_name,goods_price from goods order by goods_price desc limit 3,3;
良好的理解模型:
一、where后面的表达式,把表达式放在每一行中,看是否成立
二、字段(列),理解为变量,能够进行运算(算术运算和逻辑运算)
三、 取出结果能够理解成一张临时表
2、mysql子查询
一、where型子查询(把内层查询结果看成外层查询的比较条件)
例:查询最新的商品mysql> select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);
+----------+------------+
| goods_id | goods_name |
+----------+------------+
| 6 | 固态硬盘 |
+----------+------------+
例:查询每一个栏目下最新的产品(goods_id惟一)
mysql> select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id);
+--------+----------+-------------+
| cat_id | goods_id | goods_name |
+--------+----------+-------------+
| 1 | 2 | Dell笔记本 |
| 2 | 4 | 台式机-华硕 |
| 3 | 6 | 固态硬盘 |
+--------+----------+-------------+
二、from型子查询(把内层的查询结果供外层再次查询)
例:用子查询查出挂科两门及以上的同窗的平均成绩
-- 先查出哪些同窗挂科两门以上
mysql> select name,count(*) as gk from stuscore where score < 60 group by name having gk >=2; -- group by语句不能放在where语句前面
+------+----+
| name | gk |
+------+----+
| 刘都 | 4 |
| 周灵 | 2 |
+------+----+
-- 以上查询结果,咱们只要名字就能够了,因此再取一次名字
mysql> select name from (select name,count(*) as gk from stuscore where score < 60 group by name having gk >=2) as t;
+------+
| name |
+------+
| 刘都 |
| 周灵 |
+------+
-- 找出这些同窗了,那么再计算他们的平均分
mysql> select name,avg(score) from stuscore where name in (
select name from (select name,count(*) as gk from stuscore where score < 60
group by name having gk >=2) as t
) group by name;
+------+------------+
| name | avg(score) |
+------+------------+
| 刘都 | 57.4 |
| 周灵 | 68 |
+------+------------+
三、exists型子查询(把外层查询结果拿到内层,看内层的查询是否成立)
例:查询哪些栏目下有商品,栏目表category,商品表goods
mysql> select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
+--------+----------+
| cat_id | cat_name |
+--------+----------+
| 1 | 笔记本 |
| 2 | 台式机 |
| 3 | 硬盘 |
+--------+----------+
3、union的用法
把两次或屡次的查询结果合并起来,要求查询的列数一致,推荐查询的对应的列类型一致,能够查询多张表,屡次查询语句时若是列名不同,则取第一次的列名!若是不一样的语句中取出的行的每一个列的值都同样,那么结果将自动会去重复,若是不想去重复则要加all来声明,即union all。
现有表a以下:
id num
a 5
b 10
c 15
d 10
表b以下:
id num
b 5
c 10
d 20
e 99
例:求两个表中id相同的和
mysql> select * from a union select * from b;
+----+------+
| id | num |
+----+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| c | 10 |
| d | 20 |
| e | 99 |
+----+------+
mysql> select id,sum(num) from (select * from a union select * from b) as tmp group by id;
+----+----------+
| id | sum(num) |
+----+----------+
| a | 5 |
| b | 15 |
| c | 25 |
| d | 30 |
| e | 99 |
+----+----------+
以上查询结果在本例中的确能正确输出结果,可是,若是把表b中的b的值改成10后查询结果的b的值就是10了,由于表a中的b也是10,因此union后会被过滤掉一个重复的结果,这时就要用union all
mysql> select * from a union all select * from b;
+----+------+
| id | num |
+----+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 10 |
| c | 10 |
| d | 20 |
| e | 99 |
+----+------+
mysql> select id,sum(num) from (select * from a union all select * from b)
as tmp group by id;
+----+----------+
| id | sum(num) |
+----+----------+
| a | 5 |
| b | 20 |
| c | 25 |
| d | 30 |
| e | 99 |
+----+----------+
例:取第四、5栏目的商品,按栏目升序排列,每一个栏目的商品价格降序排列,用union完成
mysql> select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 union
select goods_id,goods_name,cat_id,goods_price from goods where cat_id=5
order by cat_id,goods_price desc;
+----------+----------------+--------+-------------+
| goods_id | goods_name | cat_id | goods_price |
+----------+----------------+--------+-------------+
| 7 | 诺基亚1520 | 4 | 4000.00 |
| 8 | 诺基亚920 | 4 | 2400.00 |
| 9 | 苹果 iPad | 5 | 3300.00 |
| 10 | 微软 Surface 2 | 5 | 3100.00 |
+----------+----------------+--------+-------------+
若是不用union,则更简单:
select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 or cat_id=5 order by cat_id,goods_price desc;
【若是子句中有order by 须要用( ) 包起来,可是推荐在最后使用order by,即对最终合并后的结果来排序】
例:取第三、4个栏目,每一个栏目价格最高的前3个商品,结果按价格降序排列
mysql> (select goods_id,goods_name,cat_id,goods_price from goods where cat_id=3 order by goods_price desc limit 3) union
-> (select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 order by goods_price desc limit 3)
-> order by goods_price desc;
+----------+------------+--------+-------------+
| goods_id | goods_name | cat_id | goods_price |
+----------+------------+--------+-------------+
| 14 | 苹果5 | 4 | 4300.00 |
| 7 | 诺基亚1520 | 4 | 4000.00 |
| 13 | 苹果4S | 4 | 3000.00 |
| 6 | 固态硬盘 | 3 | 588.00 |
| 5 | 移动硬盘 | 3 | 320.00 |
| 12 | U盘-16G | 3 | 70.00 |
+----------+------------+--------+-------------+
4、左链接,右链接,内链接
现有表a有4条数据,表b有4条数据,那么表a与表b的笛尔卡积是多少?
mysql> select * from a,b; 输出结果为4*4=16条
+----+------+----+------+
| id | num | id | num |
+----+------+----+------+
| a | 5 | b | 10 |
| b | 10 | b | 10 |
| c | 15 | b | 10 |
| d | 10 | b | 10 |
| a | 5 | c | 10 |
| b | 10 | c | 10 |
| c | 15 | c | 10 |
| d | 10 | c | 10 |
| a | 5 | d | 20 |
| b | 10 | d | 20 |
| c | 15 | d | 20 |
| d | 10 | d | 20 |
| a | 5 | e | 99 |
| b | 10 | e | 99 |
| c | 15 | e | 99 |
| d | 10 | e | 99 |
+----+------+----+------+
一、左链接
以左表为准,去右表找数据,若是没有匹配的数据,则以null补空位,因此输出结果数>=左表原数据数
语法:select n1,n2,n3 from ta left join tb on a.n1= b.n2 [这里on后面的表达式,不必定为=,也能够>,<等算术、逻辑运算符]【链接完成后,能够当成一张新表来看待,运用where等查询】
例:取出价格最高的五个商品,并显示商品的分类名称
如今的goods表以下,已将goods_price按降序排列:
mysql> select goods_id,goods_name,goods.cat_id,cat_name,goods_price from goods left join category
-> on goods.cat_id = category.cat_id order by goods_price desc limit 5;
+----------+------------+--------+----------+-------------+
| goods_id | goods_name | cat_id | cat_name | goods_price |
+----------+------------+--------+----------+-------------+
| 2 | Dell笔记本 | 1 | 笔记本 | 4600.00 |
| 14 | 苹果5 | 4 | 手机 | 4300.00 |
| 7 | 诺基亚1520 | 4 | 手机 | 4000.00 |
| 1 | 联想笔记本 | 1 | 笔记本 | 3450.00 |
| 9 | 苹果 iPad | 5 | 平板电脑 | 3300.00 |
+----------+------------+--------+----------+-------------+
二、右链接
a left join b 等价于 b right join a
推荐使用左链接代替右链接
语法:select n1,n2,n3 from a right join b on a.n1= b.n2
三、内链接
查询结果是左右链接的交集,【即左右链接的结果去除null项后的并集(去除了重复项)】
mysql目前还不支持外链接(即左右链接结果的并集,不去除null项)
语法:select n1,n2,n3 from a inner join b on a.n1= b.n2
例:现有表a
+----+------+
| id | num |
+----+------+
| a | 12 |
| b | 10 |
| c | 15 |
+----+------+
表b:
+----+------+
| id | num |
+----+------+
| d | 12 |
| e | 10 |
| f | 10 |
| g | 8 |
+----+------+
例:表a左链接表b,查询num相同的数据
mysql> select a.*,b.* from a left join b on a.num = b.num;
+----+------+------+------+
| id | num | id | num |
+----+------+------+------+
| a | 12 | d | 12 |
| b | 10 | e | 10 |
| b | 10 | f | 10 |
| c | 15 | NULL | NULL |
+----+------+------+------+
从上面能够看出,查询结果表a的列都存在,表b的数据只显示符合条件的项目
例:表b左链接表a,查询num相同的数据;a右链接表b,查询num相同的数据----这两种查询结果相同:
mysql> select a.*,b.* from b left join a on a.num = b.num;
mysql> select a.*,b.* from a right join b on a.num = b.num;
+------+------+----+------+
| id | num | id | num |
+------+------+----+------+
| a | 12 | d | 12 |
| b | 10 | e | 10 |
| b | 10 | f | 10 |
| NULL | NULL | g | 8 |
+------+------+----+------+
例:查询商品的名称,所属分类,所属品牌
先查看更改后的表结构:
表goods:
表category:
表brand:
select goods_id,goods_name,goods.cat_id,goods.brand_id,category.cat_name,brand.brand_name from goods left join category
on goods.cat_id = category.cat_id left join brand on goods.brand_id = brand.brand_id limit 5;
理解:每一次链接以后的结果均可以看做是一张新表
例:建立两张表,表m记录比赛信息,表t记录球队信息
要求查询出2006-0601至2006-07-01期间的比赛结果:
select m.*,t.* from m left join t on m.zid = t.tid;
select zid,t1.tname as t1name,res,kid,t2.tname as t2name,mtime from m left join t as t1 on m.zid = t1.tid
left join t as t2 on m.kid = t2.tid;
select zid,t1.tname as t1name,res,kid,t2.tname as t2name,mtime from m left join t as t1 on m.zid = t1.tid
left join t as t2 on m.kid = t2.tid where mtime between '2006-06-01' and '2006-07-01';
总结:能够对同一张表链接屡次,以分别取屡次数据