ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 查询语句 ### **1. 查询全部列数据** > 格式:select * from 表名; > 说明: * 表示全部列 * 案例:查询员工表(emp)的全部数据 `select * from emp;` ### **2. 查询指定列数据** > 格式: > select 列名1,列名2,...列名n from 表名; * 案例:查询emp表中,员工的编号(empno),员工姓名(ename),员工 职位(job),工资(sal) `select empno,ename,job,sal from emp;` ### **3. 给列起别名** > 格式: > select 列名1 as 别名1,列名2 as 别名2,....列名n as 别名n from 表名; > 说明:可以省略as * 案例:查询salgrade表中,工资等级(grade),最高工资(hisal),最低工资(losal)并给每列起别名 ``` select grade as 工资等级,hisal as 最高工资,losal as 最低工资 from salgrade; ``` ``` select grade 工资等级,hisal 最高工资,losal 最低工资 from salgrade; ``` * 练习:查询emp表中,员工姓名(ename),领导编号(mgr),入职时间(hiredate),部门编号(deptno) ``` select ename as 员工姓名,mgr as 领导编号,hiredate as 入职时间,deptno as 部门编号 from emp; ``` <br /> ### **4. 去掉重复的列值(去重)** > 查询出一列并去掉重复的值 > 格式: select distinct 列名 from 表名; * 案例:查询emp表中,职位(job)的种类 `select distinct job from emp; ` * 练习:查询emp表中,员工的部门编号(deptno)并去重 `select distinct deptno from emp;` <br /> ### **5. 限制查询(--重点--)** > 格式: > select */列名 from 表名 limit 初始位置,行数; > 说明:初始位置,默认值为0,表示第一行 * 案例:查询emp表中,前5行记录 `select * from emp limit 0,5; ` * 或者 `select * from emp limit 5; ` * 练习:查询emp表中,从第3行开始,共查询6条记录 `select * from emp limit 2,6;` <br /> ### **6. 排序(order by)** > 格式: > select */列名 from 表名 order by 列名1 asc/desc,列名2 asc/desc; > 说明: asc----升序排列 > desc--降序排列 如果排序中有2列进行排序,先排写在前面的,如果有相同的列值,在进行后面的排序,反之不进行。 * 案例:查询emp表中,员工编号(empno),姓名(ename),职位(job),入职时间(hiredate),根据员工编号降序排列 ``` select empno,ename,job,hiredate from emp order by empno desc; ``` * 案例:查询emp表中,员工编号,员工姓名,领导编号(mgr),部门编号(deptno) 先根据部门编号升序排列,在根据员工编号降序排列 ``` select empno,ename,mgr,deptno from emp order by deptno asc,empno desc; ``` * 练习:查询emp表中,倒数后5条记录,员工的编号,姓名,职位(job),工资(sal)---(-重点-) ``` select empno,ename,job,sal from emp order by empno desc limit 5; ``` <br /> ### **7. 条件查询** > 格式: > select */列名 from 表名 where 条件; > 条件包括:关系运算符(> < = >= <= <>/!=)、逻辑运算符(and、or、not)、特殊情况 * 案例:查询emp表中,部门编号是30号部门的,员工的编号,姓名,职位,部门编号 ``` select empno,ename,job,deptno from emp where deptno=30; ``` * 案例:查询emp表中,工资(sal)大于1000的,员工的编号,姓名,职位,工资 ``` select empno,ename,job,sal from emp where sal>1000; ``` * 练习:查询dept表中,部门地址(loc)是BEIJING的,全部信息 `select * from dept where loc='BEIJING'; ` * 练习:查询emp表中,工资(sal)小于等于3000的,员工的编号,姓名,职位,工资,根据工资升序排列。 ``` select empno,ename,job,sal from emp where sal<=3000 order by sal asc; ``` * 练习:查询emp表中,工资不等于1250的,员工的编号,姓名,工资(sal) `select empno,ename,sal from emp where sal<>1250;` #### and(与)并且--用于连接2个或者多个条件 > A and B A and B and C * 案例:查询emp表中工资在1000 ~5000之间的,员工的编号,姓名,工资 ``` select empno,ename,sal from emp where sal>=1000 and sal<=5000; ``` * 练习:查看emp表中,工资大于1000并且部门编号是30号的,员工的编号,姓名,职位,工资,部门编号 ``` select empno,ename,job,sal,deptno from emp where sal>1000 and deptno=30; ``` #### or(或)或者--用于连接2个或者条件 > A or B A or B or C * 案例:查询emp表中,工资大于2000或者部门编号是20号部门的,员工的编号,姓名,职位,工资,部门编号(deptno) ``` select empno,ename,job,sal,deptno from emp where sal>2000 or deptno=20; ``` * 练习:查询emp表中,员工编号是7521,7654,7782,7900的员工信息--(-重点-) ``` select * from emp where empno=7521 or empno=7654 or empno=7782 or empno=7900; ``` #### not(非) 取反 * 案例:查询emp表中,工资不等于1250的,员工编号,姓名,工资,根据工资降序排列-->(2种方法) > 方法1: ``` select empno,ename,sal from emp where sal<>1250 order by sal desc; ``` > 方法2: ``` select empno,ename,sal from emp where not sal=1250 order by sal desc; ``` #### 1) 查询列值为空(null)的情况(is null) * 案例:查询emp表中,奖金(comm)为空的员工信息 `select * from emp where comm is null; ` * 练习:查询emp表中,上级领导(mgr)为空的,员工信息 `select * from emp where mgr is null; ` #### 2) 查询列值不为空的情况(is not null) * 案例:查询emp表中,上级领导不为空的,员工的编号,姓名,上级领导 ``` select empno,ename,mgr from emp where mgr is not null; ``` #### 3) between....and > 格式: > select */列名 from 表名 where 列名 between 初值 and 终值; > 说明:查询初值到终值之间的数字,包含2个边界值 * 案例:查询emp表中,工资在1000 ~3000之间的,员工的编号,姓名,工资 > 方法1: ``` select empno,ename,sal from emp where sal between 1000 and 3000; ``` > 方法2: ``` select empno,ename,sal from emp where sal>=1000 and sal<=3000; ``` #### 4) in--比较一列中的几个列值 > 格式: > select */列名 from 表名 where 列名 in(列值1,列值2,....列值n); * 案例:查询emp表中,员工编号是7521,7654,7782,7900的员工信息 `select * from emp where empno in(7521,7654,7782,7900); ` * 练习:查询emp表中,员工职位(job)是 ``` SALESMAN,MANAGER,ANALYST的,员工信息 select * from emp where job in('SALESMAN' ,'MANAGER','ANALYST'); ``` #### 5) 模糊查询---(-重点-) > like 匹配/模糊匹配,会与 % 和 _ 结合使用。 > 格式:'%a' //以a结尾的数据 > 'a%' //以a开头的数据 > '%a%' //含有a的数据 > '_a_' //三位且中间字母是a的 > '_a' //两位且结尾字母是a的 > 'a_' //两位且开头字母是a的 > select 列名 from 表名 where 列名 like 条件; > * 条件包括: > (A)%----表示0个或者多个任意字符 > (B)_-----表示任意1个字符 * 案例:查询emp表中,姓名以字母M开头(首字母/第一个字母)的,员工的编号,姓名,职位 ``` select empno,ename,job from emp where ename like 'M%'; ``` * 案例:查询emp表中,姓名以字母N结尾(尾字母/最后一个字母)的,员工的编号,姓名,职位 ``` select empno,ename,job from emp where ename like '%N'; ``` * 案例:查询emp表中,姓名中包含(含有)字母N的,员工的编号,姓名,职位 ``` select empno,ename,job from emp where ename like '%N%'; ``` * 练习:查询emp表中,姓名中不包含字母N的并且部门编号是30号部门的,员工的编号,姓名,职位,部门编号 ``` select empno,ename,job,deptno from emp where ename not like '%N%' and deptno=30; ``` ``` select empno,ename,job,deptno from emp where ename not like '%N%' and deptno=30; ``` * 练习:查询emp表中,姓名倒数第2个字母是N的,员工的信息 `select * from emp where ename like '%N_'; ` * 练习:查询emp表中,姓名倒数第3个字母是N的,员工的信息 `select * from emp where ename like '%N__';` <br /> ### **8. 聚合(分组)函数** > count sum avg min max > 1) count(*/列名) > *------表示统计该表中数据总条数 > 列名--表示统计列值不为空的,数据的总条数 * 案例:统计emp表中员工的总数量 `select count(*) from emp; ` * 练习:统计emp表中,职位种类的数量 `select count(distinct job) from emp; ` >2) sum(列名) 统计某列列值累加之和 * 案例:统计emp表中,员工的工资总和 `select sum(sal) from emp; ` * 练习:查询emp表中,部门编号是30号的,工资总和 `select sum(sal) from emp where deptno=30; ` > 3) avg(列名) 求平均值 * 案例:查询emp表中,员工的平均工资 `select avg(sal) from emp; ` * 练习:查询emp表中,10号部门的平均工资 `select avg(sal) from emp where deptno=10; ` > 4) min(列名) 求最小值 * 案例:查询emp表中最低工资,并起别名显示 `select min(sal) 最低工资 from emp; ` > 5) max(列名) 求最大值 * 案例:查询emp表中,员工编号的最大值 `select max(empno) from emp;` <br /> ### **9. 分组查询** > 说明:在某张表中,根据某一列,把数据分成几组(相同的分成一组),然后对每一组数据使用聚合函数, > 聚合函数经常和分组查询一起使用。 > 格式: > select 列名/聚合函数 > from 表名 > where 条件 > group by 列名 > order by 列名1/聚合函数 asc/desc,列名2/聚合函数 asc/desc; * 案例:查询emp表中,部门的编号(deptno),部门的人数,部门的工资总和 ``` select deptno,count(*),sum(sal) from emp group by deptno; ``` * 案例:查询emp表中,工资大于1000的,部门的编号,部门的平均工资,部门的最高工资 ``` select deptno,avg(sal),max(sal) from emp where sal>1000 group by deptno; ``` * 练习:查询emp表中,工资在1000 ~5000之间的,员工的职位(job),职位的人数,职位的最高工资 ``` select job,count(*),max(sal) from emp where sal>1000 and sal<5000 group by job; ``` * 练习:查询emp表中,员工姓名不包含字母K,部门编号,部门的平均工资,部门的人数,根据部门的人数升序排列 ``` select deptno,avg(sal),count(*) from emp where not ename like '%K%' group by deptno order by count(*) asc; ``` <br /> ### **10. having语句** * 说明:在分组查询得到结果后,再次对数据进行过滤,使用having语句 > 格式: > select 列名/聚合函数 (6) > from 表名 (1) > where 条件 (2) > group by 列名 (3) > having 条件 (4) > order by 列名1/聚合函数 asc/desc,列名2/聚合函数 asc/desc; (5) * where和having区别 > (A) where是对整张表中的数据进行过滤,可以单独使用。 > (B) having是对分组后的数据进行过滤,having不可以单独使用,必须和group by一起使用。 * 案例:查询emp表中,部门的平均工资大于2000的,部门的编号,部门的平均工资,部门的人数 ``` select deptno,avg(sal),count(*) from emp group by deptno having avg(sal)>2000; ``` * 说明:where后面的条件不可以写聚合函数 > 练习:查询emp表中,工资大于1500的,职位的名称,职位的平均工资,职位的最高工资,要求职位的最高工资小于3000, 根据平均工资降序排列 ``` select job,avg(sal),max(sal) from emp where sal>1500 group by job having max(sal)<3000 order by avg(sal) desc; ``` * 练习:查询emp表中,姓名不包含字母C并且在10或者20号部门的,部门的编号,部门的人数,部门工资总和,部门的最高工资, 要求部门的最高工资大于2000,根据人数降序排列 > 方法一: ``` select deptno,count(*),sum(sal),max(sal) from emp where ename not like '%C%' and deptno in(10,20) group by deptno having max(sal)>2000 order by count(*) desc; ``` > 方法二: ``` select deptno,count(*),sum(sal),max(sal) from emp where ename not like '%C%' and (deptno=10 or deptno=20) group by deptno order by count(*) desc; ``` > 方法三: ``` select deptno,count(*),sum(sal),max(sal) from emp where ename not like '%C%' and (deptno=20 or deptno=10) group by deptno order by count(*) desc; ``` <br /> ### **11. 常用数值处理函数** #### 1) round(数值,位数) 四舍五入函数 > 如果位数>0,那么小数点后保留几位小数,如果位数=0,不保留小数,如果位数<0,那么小数点前第几位进行四舍五入 ``` select round(45.637,2); --45.64 select round(45.637,1); --45.6 select round(45.637,0); --46 select round(45.637,-1); --50 select round(45.637,-2); --0 ``` #### 2) truncate(数值,位数) 截取函数 > 如果位数>0,那么小数点后保留几位小数,如果位数=0,不保留小数,如果位数<0,那么舍弃小数点之前第几位 ``` select truncate(45.637,2); --45.63 select truncate(45.637,1); --45.6 select truncate(45.637,0); --45 select truncate(45.637,-1); --40 select truncate(45.637,-2); --0 ``` #### 3) rand(n) 随机函数 > 随机生成0~1之间的数字,如果n的数值不变,生成相同的随机数 ``` select rand(); select rand(3); ``` #### 4) sqrt(n) 平方根函数 ``` 10x10=100 select sqrt(2); --1.414 select sqrt(3); --1.732 select sqrt(9); --3 ``` #### 5) mod(n,m) 余数函数 ``` select mod(10,3); --1 select mod(10,4); --2 ``` <br /> ### **12. 常用字符处理函数** #### 1) char_length(字符串/列名) 统计字符/列值的个数(长度) * 案例:统计helloworld字符串的长度 `select char_length('helloworld'); ` * 练习:查询emp表中,员工的姓名以及姓名的字符数 `select ename,char_length(ename) from emp; ` #### 2) trim(字符串/列名) 去掉字符串或者列值两端的空格 `select trim(' 软 件 测 试 '); ` #### 3) substring(参数1,参数2,参数3) 字符串截取函数---重点 > 参数1:被截取的字符串或者列名 > 参数2:那从哪里开始截取 > 如果是正数,表示从正数第几个开始截取,如果是负数,表示从倒数第几个开始截取 > 参数3:截取的长度(个数) * 案例:截取helloworld字符串最后2个字母 ``` select substring('helloworld',-2,2); select substring('helloworld',5,3); select substring('helloworld',-6,3); ``` * 练习:查询emp表中,员工的姓名,以及姓名最后2个字母---(-重点-) > 方法1: `select ename,substring(ename,-2,2) from emp; ` > 方法2: `select ename,substring(ename,char_length(ename)-1,2) from emp;` #### 4) reverse(字符串/列名) 字符串逆序函数 `select reverse('上海自来水'); ` * 练习:查询emp表中,姓名以及姓名的逆序 `select ename,reverse(ename) from emp;` #### 5) concat(字符串/列名,字符串/列名,....) 字符串拼接 `select concat('问君能有几多愁,','恰似写完用例改需求'); ` * 练习:查询dept表中,部门名称(dname),部门地址(loc),以及部门名称和地址的拼接 `select dname,loc,concat(dname,loc) from dept;` <br /> ### **13. 常用日期时间处理函数** #### 1) curdate() 获取系统当前日期 * 案例:查询当前系统日期 `select curdate(); ` * 练习:查询昨天、今天、明天的日期 `select curdate()-1,curdate(),curdate()+1; ` #### 2) curtime() 获取系统当前时间 * 案例:查询当前系统时间 `select curtime(); ` #### 3) sysdate()/now() 获取系统当前日期时间 `select sysdate(),now(); ` #### 4) year(date) 获取年份 * 案例:获取当前系统的年份 `select year(sysdate());` * 练习:查询emp表中,1981年入职的员工信息--->(-重点-) `select * from emp where year(hiredate)='1981';` #### 5) month(date) 获取月份 * 案例:获取当前系统的月份 `select month(now()); ` * 练习:查询emp表中,12月份入职的员工信息--->(-重点-) `select * from emp where month(hiredate)='12';`