1. 程式人生 > 實用技巧 >SQL——SQL語句總結(7)

SQL——SQL語句總結(7)

單表查詢

資料庫中包含大量的資料,根據特殊要求,可能只需要查詢表中的指定資料,即對資料進行過濾在 select 語句中,通過 where 子句可以對資料進行過濾。

select [all | distinct] * | column_name 
from table_name 
where <條件表示式>;

下面總結:

開始建表

建立部門表(dept)

create table dept(
    `deptno` int(2) not null comment '部門編號',
    `dname` varchar(14) comment '部門名稱',
    `loc` 
varchar(13) comment '部門所在地' );

插入表資料

insert into dept (deptno, dname, loc) 
values (10, 'ACCOUNTING', 'NEW YORK'),
       (20, 'RESEARCH', 'DALLAS'),
       (30, 'SALES', 'CHICAGO'),
       (40, 'OPERATIONS', 'BOSTON');

建立員工表(emp)

create table emp
(
    `empno` int(4) not null comment '員工編號',
    `ename` 
varchar(10) comment '員工姓名', `job` varchar(9) comment '員工職位', `mgr` int(4) comment '上級編號', `hiredate` date comment '入職日期', `sal` int(7) comment '員工工資', `comm` int(7) comment '員工獎金', `deptno` int(2) comment '部門編號' );

插入表資料

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) 
values (7369, 'SMITH', 'CLERK', 7902, str_to_date('17-12-1980', '%d-%m-%Y'), 800,null,20), (7499, 'ALLEN', 'SALESMAN', 7698, str_to_date('20-02-1981', '%d-%m-%Y'),1600, 300, 30), (7521, 'WARD', 'SALESMAN', 7698, str_to_date('22-02-1981', '%d-%m-%Y'),1250, 500, 30), (7566, 'JONES', 'MANAGER', 7839, str_to_date('02-04-1981', '%d-%m-%Y'),2975, null, 20), (7654, 'MARTIN', 'SALESMAN', 7698, str_to_date('28-09-1981', '%d-%m-%Y'),1250, 1400, 30), (7698, 'BLAKE', 'MANAGER', 7839, str_to_date('01-05-1981', '%d-%m-%Y'),2850, null, 30), (7782, 'CLARK', 'MANAGER', 7839, str_to_date('09-06-1981', '%d-%m-%Y'),2450, null, 10), (7788, 'SCOTT', 'ANALYST', 7566, str_to_date('19-04-1987', '%d-%m-%Y'),3000,null, 20), (7839, 'KING', 'PRESIDENT', null, str_to_date('17-11-1981', '%d-%m-%Y'),5000,null, 10), (7844, 'TURNER', 'SALESMAN', 7698, str_to_date('08-09-1981', '%d-%m-%Y'),1500, 0, 30), (7876, 'ADAMS', 'CLERK', 7788, str_to_date('23-05-1987', '%d-%m-%Y'),1100,null, 20), (7900, 'JAMES', 'CLERK', 7698, str_to_date('03-12-1981', '%d-%m-%Y'),950,null, 30), (7902, 'FORD', 'ANALYST', 7566, str_to_date('03-12-1981', '%d-%m-%Y'),3000,null, 20), (7934, 'MILLER', 'CLERK', 7782, str_to_date('23-01-1982', '%d-%m-%Y'),1300,null, 10);

建立工資等級表(salgrade)

create table salgrade (
    `grade` numeric primary key comment '工資等級',
    `losal` numeric comment '最低工資',
    `hisal` numeric comment '最高工資'
);

插入表資料

insert into salgrade
 values (1, 700, 1200),
        (2, 1201, 1400),
        (3, 1401, 2000),
        (4, 2001, 3000),
        (5, 3001, 9999);

新增外來鍵

alter table EMP add constraint PK_EMP primary key (EMPNO);
alter table EMP add constraint FK_DEPTNO foreign key (DEPTNO) references DEPT (DEPTNO);

表建完了!

查詢emp全部資訊

select * from emp;

結果:

查詢dept全部資訊

select * from dept;

結果:

查詢salgrade全部資訊

select * from salgrade;

結果:

1.查詢指定記錄

例題:

(1)查詢編號為10的員工資訊;

select * from emp where dpetno = 10;

結果:

(2)查詢獎金為null的人員姓名與工資

select ename, sal from emp where comm is null;

結果:

(3)查詢年薪大於3萬的人員的姓名與部門編號

select ename, deptno, sal*12+ifnull(comm,0) '年薪' from emp where sal*12+ifnull(comm,0) > 30000; 

 ifnull(n1, n2) 函式 作用為當 n1 為 null 時,函式返回 n2,反之,當 n1 不為 null 時,返回 n1 本身。

結果:

(4)查詢工資大於1500

select * from emp where sal >1500;

結果:

(5)查詢emp表顯示工資超過2850的員工姓名和工資

select ename, sal from emp where sal > 2850;

結果:

(6)查詢emp表員工編號為7566的員工及所在部門編號

select ename,deptno from emp where empno = 7566;

結果:

2.帶 in 關鍵字的查詢

(1)查詢emp表中員工在1或3部門的人員資訊

select * from emp where deptno in(10, 30);

結果:

(2)查詢顯示不存在僱員的所有部門號

select d.deptno from dept d where d.deptno not in (select distinct deptno from emp);

distinct 關鍵字為去重。

結果:

3.帶 between and 的範圍查詢

(1)查詢emp表顯示在1981年2月1日到1981年5月1日之間僱傭的僱員名、崗位及僱用日期,並以僱傭日期進行排序。

select ename,job,hiredate from emp where hiredate between '1981-02-01' and '1981-05-01' order by hiredate asc;

結果:

(2)查詢81年入職的員工資訊

select * from emp where hiredate between '1981-01-01' and '1981-12-31';

結果:

4.帶 like 的字元匹配查詢

like 關鍵字需要與萬用字元一起使用,萬用字元為 '%' 和 '_'。

  '%' 匹配任意長度的字元,甚至包含零字元

  '_' 一次只能匹配任意一個字元

(1)查詢姓名裡面含有S員工資訊 工資 姓名

select sal, ename from emp where ename like('%s%');

結果:

(2)查詢emp表顯示第2個字元為“A”的所有僱員名及其工資

select ename,sal from emp where ename like('_A%');

結果:

(3)求姓名以J開頭第二個字元O的員工姓名與工資

select ename, sal from emp where ename like('jo%');

結果:

(4)求包含%的僱員姓名

  因表裡沒有員工姓名裡有百分號,為完成此題,需要給emp表中新增一條資料,如下:

insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 
values (7984, 'xiao%01', 'mansger', 7839, '1991-10-05', 1005, 1005, 30);
select ename from emp where ename like('%/%%') escape('/');

  escape關鍵字為轉義。

結果:

學習完成可以刪掉這條資料!

5.查詢空值

(1)查詢沒有獎金的員工資訊

select * from emp where comm is null;

結果:

表中的資料有一條 comm 為 0 的資料,注意 這裡的獎金為 0 ,並不是沒有獎金。

(2)查詢工資大於1500且含有佣金的人員資訊

select * from emp where sal >1500 and comm is not null;

結果:

6.帶 and 的多條件查詢

(1)查詢部門為30且有獎金的員工資訊

select * from emp where deptno = 30 and comm is not null;

結果:

7.帶 or 的多條件查詢

(1)查詢emp表顯示工資不在1500~2850之間的所有僱員及工資

select ename, sal from emp where sal < 1500 or sal > 2850;

結果:

(2)查詢emp表顯示部門10和30中工資超過1500的僱員名及工資

select ename,sal from emp where (deptno = 10 or deptno = 30) and sal >1500;

  and 的優先順序大於or

結果:

8.對查詢結果排序

(1)按照員工編號降序查詢全部資訊

select * from emp order by empno desc;

  desc 關鍵字為降序。

結果:

(2)按照員工入職時間升序查詢全部資訊

select * from emp order by hiredate asc;

  asc 關鍵字為升序。

結果:

9.分組查詢

分組查詢是對資料按照某個或多個欄位、表示式、列編號進行分組,MySQL中使用 group by 關鍵字對資料進行分組。

基本語法:

[ group by {欄位名 |表示式 |列編號} [asc |desc],···[with rollup] ]
[ having<分組條件表示式> ]

(1)建立分組

group by 從句根據所給的欄位名返回分組的查詢結果,可用於查詢具有相同值的欄位。

根據根據部門查詢全部資訊

select * from emp group by deptno;

結果:

group by 關鍵字通常和聚合函式一起使用,如 max() 、min()、count()、sum()、avg()。

 1)部門下員工的工資>2000的人數

select deptno, count(*) from emp where sal > 2000 group by deptno;

結果:

 2)查詢各部門人數

select deptno, count(*) from emp group by deptno;

結果:

 3)求部門裡面工齡最大和工齡最小的

select deptno, min(hiredate), max(hiredate) from emp group by deptno;

結果:

拓展:

在 3)的基礎上,要知道人員姓名

select mm2.deptno, e1.ename, e1.hiredate
from emp e1,
    (select min(hiredate) mind, max(hiredate) maxd, e.deptno 
  from emp e   group by e.deptno) mm2 where e1.hiredate = mm2.mind or e1.hiredate = mm2.maxd;

結果:

 4)求部門薪水最高

select deptno ,max(sal) from emp group by deptno; 

結果:

 5)求部門薪水最低

select deptno, min(sal) from emp group by deptno;

結果:

 6)求部門平均薪水

select deptno ,avg(sal) from emp group by deptno; 

結果:

 7)查詢各個部門員工的工資之和

select deptno, sum(sal) from emp group by deptno;

結果:

(2)使用having過濾分組

使用 group by 對錶中的資料分組後,可以通過 having 子句對分組後的資料進行條件篩選。

where 與 having 的區別

  1.where 不能與聚合函式連用。

  2.where 在分組前過濾,having在分組後過濾。

  3.where 排除的記錄不再包含在分組中。

 1)查詢部門人數大於3的部門及部門人數

select deptno, count(*) from emp group by deptno having count(*) > 3;

結果:

(3)在 group by 子句中使用 with rollup

使用 with rollup 關鍵字之後,在所有查詢出的分組紀錄之後增加一條記錄,該記錄計算查詢出的所有記錄總和,及統計記錄數量。

 1)查詢每個部門的人數

select deptno, count(*) from emp group by deptno with rollup;

結果:

(4)多欄位分組

使用 group by 可以對多個欄位進行分組,group by關鍵字後面跟需要分組的欄位,MySQL根據多欄位的值進行層次分組。分組層次從左到右。

 1)根據部門編號和員工職位查詢全部資訊

select * from emp group by deptno, job;

結果:

(5)group by 和 order by 一起使用

某些情況下需要對分組進行 排序,order by 用來對查詢記錄排序,如果和 group by 一起使用,可以完成對分組之後的資料進行排序。

 1)求部門的平均工資,由高到低。

select deptno, avg(sal) from emp group by deptno order by avg(sal) desc;

結果: