--Logical Operators
- and
- select * from classes where age >= 18 and age <= 28;
- or
- select * from classes where age>18 or height >=100;
- not
- select * form classes where not (age > 18 and gender=2);
-- Fuzzy query (where name like the data to be queried)
- like
- % replace any
- _ replace 1
- Search for names starting with "small"
- select * from classes where name like "小%"
-- Range query
- in (1,3,6) means in a non-contiguous range
- Search for names with ages 18 and 34
- select * from classes where age in (18,34);
- not in non-contiguous range
- select * from classes where age between 18 and 34;
- not between...and.. means not in a continuous range
- Empty judgment is null and non-null judgment is not null
- select * from classes where height is null
--sort
- order by
- asc sort from small to large, ascending order
- desc sort from largest to smallest, descending
- select * from classes where (age between 18 and 34) and gender = 1 order by age asc,name desc;
--aggregate function
- total
- -count
- select count(*) from classes where gender = 1;
- maximum value
- max
- minimum
- min
- sum
- sum
- average value
- avg
- round(123.23 , 1) round to 1 decimal place (rounding)
--grouping (can be used with aggregate functions)
- group by
- Group by gender, query all genders
- select gender from classes group by gender;
- group_concat(...)
- 查询同种性别中的姓名
- select group_concat(name),gender from classes group by gender;
- having(having 类似where和group by组合使用)
- 查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30
- select gender,group_concat(name) from students group by gender having avg(age) > 30;
--分页
- limit start,count
- select * from classes limit 0,2;
--连接查询
- inner join ... on
- select ... from 表A inner join 表B;
- select * from students inner join classes on students.cls_id = classes.id;
- left join(左连接)
- right join(右连接)
- 以左边或者右边的表作为基准进行查询,没有的信息将以null显示
-- 子查询
- 标量子查询:子查询返回的结果是一个数据(一行一列)
- 列子查询:返回的结果是一列(一列多行)
- 行子查询:返回的结果是一行(一行多列)
- 1 查出平均身高
- select avg(height) from students ; -- 172
- 2 查出高于平均身高的信息
- select * from students where height >(select avg(height) from students) ;