roro
查询语句是数据库中,使用最多,最频繁的语句
1.基础查询语句
查询sin表中,所有数据 “ * ” 代表所有
select * from sin;
也可以只查询其中一个,或者几个字段。查询sin表中的name,id数据
select name,id from sin;
as是别名的意思,从sin表中获取joindate数据,并给joindate取别名为date
select joindate as date from sin;
distinct关键字会过滤重复的值,多个值相同,只返回一次。从sin表中获取age数据,重复年龄只出现一次
select distinct age from sin ;
把distinct和as一起使用
select distinct joindate as '出道日期' from sin;
2.条件查询语句
where 后加限制条件
select * from sin where age=40;
null是空的意思,查询gender不为空的所有数据
select * from sin where gender is not null;
几种简单的条件查询
select * from sin where age != 40;
select * from sin where age between 39 and 41;
select * from sin where age <35 and gender='男';
select * from sin where age = 32 ||age =36||age=41;
select * from sin where age in(32,36,41);
like是模糊查询,'_'代表一个字符,'%'代表多个字符
查询名字为两个字的歌手数据 查询名字为三个字的歌手数据 查询最后一个字是“谦”的歌手数据
select * from sin where name like '__';
select * from sin where name like '___';
select * from sin where name like '%谦';
3.聚合函数
count() 计算括号指定字段中数据的个数
查询sin表中name字段中有几条数据 查询sin表中一共有多少条数据。由于每一条数据都有name,所以查询结果相同
select count(name) from sin;
select count(*) from sin;
avg: 平均值 max:最大值 min:最小值 sum:求和
select avg(age) from sin;
select max(age) from sin;
select min(age) from sin;
select sum(age) from sin where id <=2;