离散查询
select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)取出数据
select * from car where price not in(30,40,50,60)去掉数据
聚合函数(统计查询)
select count(*) from car
select count(code) from car #取所有的数据条数
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值
分页查询
select * from car limit 0,10 #分页查询,跳过几条数据(0)取几条(10)
规定一个每页显示的条数:m
当前页数:n]
select * from car limit (n-1)*m,m
去重查询
select distinct brand from car
分组查询
查询汽车表中,每个系列下汽车的数量
select brand,count(*) from car group by brand
分组之后,只能查询该列或聚合函数
取该系列价格平均值大于40的系列代号
select brand from car group by brand having(加条件) avg(price)>40
取该系列油耗最大值大于8的系列代号
select brand from car group by brand having max(oil)>8
高级查询
(1)连接查询
SELECT t1.`Name`,t2.Brand_Name FROM brand t2,car t1 -- 笛卡尔乘积
WHERE t2.Brand = t1.Brand
-- 多表连接查询
SELECT t1.`Name`,t2.Brand_Name,t3.prod_name
FROM car t1
LEFT JOIN brand t2
ON t1.Brand = t2.Brand
LEFT JOIN productor t3 ON t2.Prod = t3.Prod
(2) 联合查询 字段数必须一样
SELECT `Name`,Price FROM car
UNION
SELECT Brand_Name,Brand_Memo FROM brand
(3)子查询
SELECT * FROM car
WHERE car.brand in
(SELECT Brand FROM brand WHERE Prod = 'p001')