mysql>select ... from ... where ... group by ... having ... order by ... limit ..
mysql>-- 去重查询
mysql>selectdistinctprice,cid from orders ;
mysql>-- 条件查询
mysql>select price,cid from orderswhereprice > 12.27 ;
mysql>-- 聚集查询
mysql>select max(price) from orders ;
mysql>select min(price) from orders ;
mysql>select avg(price) from orders ;
mysql>select sum(price) from orders ;
mysql>select count(id) from orders ;
mysql>-- 分组查询
mysql>select max(price) from orders where cid is not nullgroup bycid ;
mysql>-- 分组查询(组内过滤)
mysql>select cid ,orderno,max(price) as max_price,min(price) from orders where cid is not null group by cidhaving max_price > 20 ;
mysql>-- 降序查询
mysql>select cid ,orderno,max(price) as max_price,min(price) from orders where cid is not null group by cid having max_price > 20 order by max_price desc;
mysql>--模糊查询
mysql>select * from customers where namelike'toma%'
mysql>select * from customers where namenot like'toma%'
mysql>--范围查询
mysql>select * from customers where idin(1,2,3)
mysql>select * from customers where idnot in(1,2,3)
mysql>-- between 1 and 10,闭区间
mysql>select * from customers where id between 1 and 3 ;
mysql>select * from customers where id >= 1 and id <= 3 ;
mysql>-- 嵌套子查询(查询没有订单的客户)
mysql>select * from customers where id not in (select distinct cid from orders where cid is not null);
mysql>-- 嵌套子查询(查询订单数量>2的客户)
mysql>select * from customers where id in (select cid from orders group by cid having count(cid) > 2);
mysql>select * from customers where id in ( select t.cid from (select cid,count(*) as c from orders group by cid having c > 2) as t);
mysql>-- 嵌套子查询(查询客户id,客户name,订单数量,最贵订单,最便宜订单,平均订单价格 where 订单数量>2的客户)
mysql>select a.id,a.name,b.c,b.max,b.min,b.avg
from customers a,((select cid,count(cid) c , max(price) max ,min(price) min,avg(price) avg from orders group by cid having c > 2) as b)
网友评论