目錄
1. 數據庫的基本查詢
1.1 簡單的記錄查詢
1.2 使用列別名
2.??數據分頁查詢
(1)查詢前五行數據
(2)查詢 11 ~? 15 行數據
3. 結果集排序
3.1 單關鍵字排序
(1)升序排列
(2)降序排列
(3)按照名字降序排列
3.2 多關鍵字排序
(1)按照 薪資降序 + 入職日期升序 ?查詢
(2)部門升序 + 薪資降序 查詢
3.3?排序 + 分頁
(1)薪資 默認升序排列 1 ~ 5 行數據
4. 去除重復記錄
4.1 去除 job 的相同行數據
4.2 去除 job與其他關鍵字聯合?的相同行數據
5. 條件查詢
(1)查詢部門編號為20 ?且薪資大于1500 的員工信息
(2)查詢部門編號為20 或 30, 且薪資大于 1300 的員工信息
(3)查詢部門編號為 10 或 30,且年收入大于17000, 且工齡超過20年
(4)查詢部門編號為 10、20、30, 且 工作不是(salesman 或 president),且入職年月早于“1995-01-01”
(5)?查詢獎金(comm)為空 的員工信息(帶小節)
(6)查詢獎金(comm)不為空 的員工信息
(7)查詢獎金(comm)不為空 薪資在1500與2500之前 的員工信息
(8)?查詢獎金(comm)不為空 且薪資在1500與3000之前 姓名中包含A 的員工信息
(9)查詢姓名為“__RD”的員工
(10)通過正則表達式查詢 ?長度為2~4個字的漢字字符
(11) 邏輯運算符
(12)查詢 員工編號不為10、20 的員工信息
(13)查詢 (員工編號不為10、20) 異或 (薪資大于1800) 的員工信息?
(14) 按位運算符
(15) where 子句的注意事項
6. 各種子句的執行順序
前置知識總結:
示例數據庫文件:
通過網盤分享的文件:demo.sql
鏈接: https://pan.baidu.com/s/1iUKSMScC1PXtbOFFaKm-vQ 提取碼: i5hb 復制這段內容后打開百度網盤手機App,操作更方便哦
1. 數據庫的基本查詢
1.1 簡單的記錄查詢
select * from t_emp;
select empno, ename, sal, deptno from t_emp;
1.2 使用列別名
select empno, ename, sal*14 as "year-income" from t_emp;
2.??數據分頁查詢
(1)查詢前五行數據
方式一:
# 查詢 1 ~ 5 行數據
select empno, ename, sal, deptno from t_emp limit 0,5;
?方式二:
# 查詢 1 ~ 5 行數據
select empno, ename, sal, deptno from t_emp limit 5;
(2)查詢 11 ~? 15 行數據
# 查詢 11 ~ 15 行數據
select empno, ename, sal, deptno from t_emp limit 10,5;
3. 結果集排序
3.1 單關鍵字排序
(1)升序排列
select empno, ename, sal from t_emp order by sal;select empno, ename, sal from t_emp order by sal asc;
(2)降序排列
# 按照薪資 降序排列
select empno, ename, sal from t_emp order by sal desc;
(3)按照名字降序排列
3.2 多關鍵字排序
(1)按照 薪資降序 + 入職日期升序 ?查詢
# 按照 薪資降序 + 入職日期升序 查詢
select empno, ename, sal, hiredate
from t_emp
order by sal desc, hiredate asc;
(2)部門升序 + 薪資降序 查詢
# 按照 部門升序 + 薪資降序 查詢
select empno, ename, sal, deptno
from t_emp
order by deptno asc, sal desc;
3.3?排序 + 分頁
(1)薪資 默認升序排列 1 ~ 5 行數據
# 按照薪資 默認升序排列 1 ~ 5 行數據
select empno, ename, sal from t_emp order by sal limit 5;
4. 去除重復記錄
4.1 去除 job 的相同行數據
select job from t_emp;
# 去除 job 的相同行數據
select distinct job from t_emp;
4.2 去除 job與其他關鍵字聯合?的相同行數據
select job, sal from t_emp;
# 去除 job與其他關鍵字聯合?的相同行數據
select distinct job, sal from t_emp;
5. 條件查詢
(1)查詢部門編號為20 ?且薪資大于1500 的員工信息
# 查詢部門編號為20 且薪資大于1500 的員工信息
select empno, ename, sal, job from t_emp
where deptno = 20 and sal >= 1500;
(2)查詢部門編號為20 或 30, 且薪資大于 1300 的員工信息
# 查詢部門編號為20 或 30, 且薪資大于 1300 的員工信息
select empno, ename, sal, jobfrom t_emp
where (deptno = 20 or deptno = 30) and sal >= 1300;
(3)查詢部門編號為 10 或 30,且年收入大于17000, 且工齡超過20年
# 查詢部門編號為 10 或 30,且年收入大于17000, 且工齡超過20年
select empno, ename, sal, jobfrom t_emp
where (deptno = 10 or deptno = 30)
and (sal + ifnull(comm, 0)) * 12 >= 17000
and datediff(now(), hiredate)/365 >= 20;
(4)查詢部門編號為 10、20、30, 且 工作不是(salesman 或 president),且入職年月早于“1995-01-01”
# 查詢部門編號為 10、20、30, 且 工作不是(salesman 或 president),且入職年月早于“1995-01-01”
select *from t_emp
where deptno in(10, 20, 30)
and (job != "SALESMAN" and job != "PRESIDENT" )
and hiredate < "1982-01-01";
(5)?查詢獎金(comm)為空 的員工信息(帶小節)
# 查詢獎金(comm)為空 的員工信息
select *from t_emp
where comm is null;
(6)查詢獎金(comm)不為空 的員工信息
# 查詢獎金(comm)不為空 的員工信息
select *from t_emp
where comm is not null;
(7)查詢獎金(comm)不為空 薪資在1500與2500之前 的員工信息
# 查詢獎金(comm)不為空 薪資在1500與2500之前 的員工信息
select *from t_emp
where comm is not null
and sal between 1500 and 2500;
(8)?查詢獎金(comm)不為空 且薪資在1500與3000之前 姓名中包含A 的員工信息
# 查詢獎金(comm)不為空 且薪資在1500與3000之前 姓名中包含A 的員工信息
select *from t_emp
where comm is not null
and sal between 1500 and 3000
and ename like "%A%";
(9)查詢姓名為“__RD”的員工
# 查詢姓名為“__RD”的員工
select *from t_emp
where ename like "__RD"
(10)通過正則表達式查詢 ?長度為2~4個字的漢字字符
# 通過正則表達式查詢 長度為2~4個字的漢字字符
select *from t_emp
where ename
regexp "^[\\u4e00-\\u9fa5]{2,4}$";
(11) 邏輯運算符
(12)查詢 員工編號不為10、20 的員工信息
# 查詢 員工編號不為10、20 的員工信息
select *
from t_emp
where not deptno in(10, 20);
(13)查詢 (員工編號不為10、20) 異或 (薪資大于1800) 的員工信息?
# 查詢 (員工編號不為10、20) 異或 (薪資大于1800) 的員工信息
select empno, ename, deptno, sal
from t_emp
where not deptno in(10, 20)
xor sal >= 1800;
(14) 按位運算符
14.1 3&7
14.2 3|7
14.3 ~10
14.4 3^7
14.5? 10 << 1
15.6 10>>1