一、数据查询语句:DQL

  作用:查询

二、DQL

  #基本查询(可不加where 条件)

  select * from 表名 where 条件;

  select 列1,列2 from 表名 where 条件;

  #过滤掉重复列

  select distinct 列1 from 表名;

  #合并为一行

  select concat (列1,列2) from 表名;

  select concat_ws ('==',列1,列2) from 表名;

  #改名

  slect 列1 as 别名,列名2 as 别名2 from 表名;

  #模糊查询(%匹配所有,_匹配一个)

  select 列名 from 表名 where 列名 like '%xx_';

  #聚合函数(函数中只能是列名或*)

  #表中记录的条数

  select count(*) from 表名;

  #查询此列的和

  select sum(列名) from 表名;

  #查询此列的平均值

  select avg(列名) from 表名;

  #查询此列的最大值

  select max(列名) from 表名;

  #查询此列的最小值

  select min(列名) from 表名;

  #分组查询(having筛选的是分组后的虚拟表格)

  select 列名 from 表名 group by 分组列名;

  select 列名 from 表名 group by 分组列名 having 条件;

  select 列名 from 表名 where 条件 group by 分组列名;

  select 列名 from 表名 where 条件 group by 分组列名 having 条件;

  #多列分组

  select 列1,列2 from 表名 group by 分组1,分组2; 

  #连接查询

  #内连接(inner join)

  例:select s.sno,s.name,c.cno,c.score from atu as s inner join sc as c on s.sno=c.sno;

  #外连接

  #左连接(left join)

  例:select s.sno,s.name,c.cno,c.score from atu as s left join sc as c on s.sno=c.sno;

  #右连接(right join)

  例:select s.sno,s.name,c.cno,c.score from atu as s rigth join sc as c on s.sno=c.sno;

  #联合查询(union:自动去重,union all:不去重)

  select 列1,列2 from 表名1 union select 列1,列2 from 表名2;

  select 列1,列2 from 表名1 union all select 列1,列2 from 表名2;

  #限制查询行数

  #查询3行

  select * from 表名 limit 3;

  #从索引3开始查询5行

  select * from 表名 limit 3,5;

  #排序

  #升序

  select * from 表名 where 条件 order by 列名 asc; 

  #降序

  select * from 表名 where 条件 order by 列名 desc;

  #子查询

  #相关子查询

  例:select sname,sno from stu in (select sno from sc group by sno having avg(score) > 60);

  #非相关子查询

  例:select student.sname,student.sno from student,sc where student.sno = sc.sno and sc.score >= 60 group by sc.sno having count(sc.sno) >= 2;

  #any(>最小值,<最大值)

  select 列名 from 表名 where 条件 > any (select 列名 from 表名 where 条件);

  #all(>最大值,<最小值)

  select 列名 from 表名 where 条件 > all (select 列名 from 表名 where 条件);

posted on 2020-01-13 20:17  badbadboy  阅读(235)  评论(0编辑  收藏  举报