DQL(数据查询语言)

2017-07-18 14:20:47
数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE
子句组成的查询块:
SELECT <字段名表>
FROM <表或视图名>
WHERE <查询条件>


1
-- ****************** 1 distinct, 列运算, null运算, 字符串相加 ********************************************************* 2 -- 1.1去重复字段 distinct 3 select distinct name from student; 4 5 -- 1.2列运算 6 select name, age+1 as age from student; 7 8 -- 1.3null运算 (null 与任何相加都是 null) 9 -- select address+1 address from student; 10 select ifnull(address,0)+1 address from student; 11 12 -- 1.4字符串相加 (null 与任何相加都是 null) 13 select concat(address,'str') address from student; 14 15 -- ***************** 2 查看表数据推荐的方法 ****************************************************************************** 16 17 -- 2.1 同时查询多张表 18 select * from dept; 19 select * from stu; 20 select * from emp; 21 22 -- ******************* 3 运算符,通配符 **************************************************************************************** 23 24 -- 3.1测试 where 中使用的运算符:(=,<>,>,<,>=,<=,between and,in,or,null,not null,like) 25 select * from emp where job = 'clerk'; 26 27 select * from emp where job <> 'clerk'; 28 -- select * from emp where job != 'clerk'; 29 select * from emp where sal > 20000; 30 select * from emp where sal < 10000; 31 select * from emp where sal between 10000 and 20000; 32 33 select * from emp where mgr = 1004 or mgr = 1009; 34 select * from emp where mgr in(1004,1009); 35 36 select * from emp where comm is null; 37 select * from emp where comm is not null; 38 39 select * from emp where mgr = 1009 and sal > 20000; 40 41 -- 3.2 模糊查询like+(通配符:%,_) 42 select * from emp where ename like 'z_______';#共8个字符,以z开头(_ :代替一个字符) 43 select * from emp where ename like 'z%';#以z开头的字符 44 select * from emp where ename like '%z%';#查询姓名中包含z(%:代替 0或多个字符) 45 -- select * from student where name like '%c%'; 46 47 48 -- ******************* 4排序 ********************************************************************************************** 49 50 -- 4.1单一排序(默认升序) 51 select ename,sal from emp order by sal desc;#(从大到小) 52 select ename,sal from emp order by sal asc;#(从小到大) 53 select ename,sal from emp order by sal ; 54 -- 4.2结合排序 55 select ename,sal,comm from emp order by sal asc, comm desc;#先按照sal升序再按照common降序,若sal相同 则按照common排序 56 -- select ename,sal,comm from emp order by sal , comm desc; 57 58 -- ******************* 5聚合函数 ,group by, limit *************************************************************************** 59 -- 聚合函数(聚合函数常常需要添加 GROUP BY 语句。)WHERE 关键字无法与 Aggregate 函数一起使用,增加 了 having语句。 60 61 -- 5.1 count() 62 select count(*) '公司人数'from emp;#14 *:所有的记录数,忽略了空(这里为公司人数) COUNT(*) 返回被选行数 63 select count(mgr) mgr from emp;#13 指定列号:不为null的记录数 COUNT(column) 返回某列的行数(不包括 NULL 值) 64 -- SELECT COUNT(comm), COUNT(mgr) FROM emp; 65 66 -- 5.2 sum() avg() 67 select sum(sal) from emp where sal > 20000; 68 select avg(sal) from emp ; 69 70 -- 5.3 max(),min() 71 select max(sal) max , min(sal) min from emp; 72 73 select count(*) as '公司人数' , sum(sal) as '工资总和' 74 from emp; 75 76 select min(sal) ,job as '岗位' from emp group by job ; 77 78 -- 5.4 having 79 select job,avg(sal) ,count(*) from emp group by job having count(*) < 3 and avg(sal)> 20000;# 平均工资大于20000人数小于3 的工种数据 80 81 -- 5.5 limit(分页) 82 select * from emp limit 0,10;# emp 表中的前10 行数据 83 84 select * from emp order by sal desc limit 0,5;# emp 表中工资前5名 85 86 select * from emp order by sal desc limit 5,5;# emp 表工资为6 ~ 10 名

-- 子查询

where 型子查询:内层sql的返回值在where后作为条件表达式的一部分 
例句: select * from tableA where colA = (select colB from tableB where ...); 

from 型子查询:内层sql查询结果,作为一张表,供外层的sql语句再次查询 
例句:select * from (select * from ...) as tableName where .... 

 

转载于:https://www.cnblogs.com/qmqn/p/7200255.html

你可能感兴趣的:(DQL(数据查询语言))