1.模糊查询
% 包含零个或更多字符的任意字符串
_ 任何单个字符
select * from t_class where c_add like ’%路%’;#表示查询表中的地址中包含有“路”字的数据。
select * from t_class where c_add like '路%'; #表示查询表中的地址以“路”字开头的数据。
select * from t_class where c_add like '%路'; #表示查询表中的地址以“路”字结尾的数据。
select * from t_class where c_name like '__'; #表示查询表中的名字是两个字的数据。
2.过滤空值数据
select * from t_class where c_age is /is not null; #表示查询表中的年龄是空/不是空的数据。
3.升序排列asc 和降序排列 desc
select * from t_class where c_age is not null order by c_age; #表示查询表中的年龄以升序排列的数据。
select * from t_class where c_age order by c_name desc; #表示查询表中的年龄和名字以降序排列的数据。
4.聚和函数:用于查询统计数据。
count 对行计数,包括空值。
select count (<计数规范>)from 表名
select count (s_id) from t_class; #表示计算s_id的行数。
select count (distinct s_name) from t_student;
排除重复数据和非空数据。
select sum (all s_age )from t_student ;
聚合函数sum求某列数据的总和。
select avg (all s_age )from t_student ;#求平均值
ps :avg不会计算空值,如果十个人中有一个人的数据是空值,那么计算的时候就会只计算9个人的值,计算出来的结果可能跟自己想要的不一样。可以用sum/count(*)的方式来自己计算。
5.Max返回最大值 Min返回最小值
select max (s_age) from t_student ;
select min (s_age) from t_student;
ps:不可加在where后面,不可查询其他列。
数据分组:group by 加在where 条件后分组。
select s_age count (s_age) from t_student where s_age >18 group by s_age;
先按班分组,然后求每个组学生的总和。
select s_s_id ,count (s_id) from t_student group by s_c_id;
% 包含零个或更多字符的任意字符串
_ 任何单个字符
select * from t_class where c_add like ’%路%’;#表示查询表中的地址中包含有“路”字的数据。
select * from t_class where c_add like '路%'; #表示查询表中的地址以“路”字开头的数据。
select * from t_class where c_add like '%路'; #表示查询表中的地址以“路”字结尾的数据。
select * from t_class where c_name like '__'; #表示查询表中的名字是两个字的数据。
2.过滤空值数据
select * from t_class where c_age is /is not null; #表示查询表中的年龄是空/不是空的数据。
3.升序排列asc 和降序排列 desc
select * from t_class where c_age is not null order by c_age; #表示查询表中的年龄以升序排列的数据。
select * from t_class where c_age order by c_name desc; #表示查询表中的年龄和名字以降序排列的数据。
4.聚和函数:用于查询统计数据。
count 对行计数,包括空值。
select count (<计数规范>)from 表名
select count (s_id) from t_class; #表示计算s_id的行数。
select count (distinct s_name) from t_student;
排除重复数据和非空数据。
select sum (all s_age )from t_student ;
聚合函数sum求某列数据的总和。
select avg (all s_age )from t_student ;#求平均值
ps :avg不会计算空值,如果十个人中有一个人的数据是空值,那么计算的时候就会只计算9个人的值,计算出来的结果可能跟自己想要的不一样。可以用sum/count(*)的方式来自己计算。
5.Max返回最大值 Min返回最小值
select max (s_age) from t_student ;
select min (s_age) from t_student;
ps:不可加在where后面,不可查询其他列。
数据分组:group by 加在where 条件后分组。
select s_age count (s_age) from t_student where s_age >18 group by s_age;
先按班分组,然后求每个组学生的总和。
select s_s_id ,count (s_id) from t_student group by s_c_id;