mysql基礎(三)——中級查詢


創建表

CREATE TABLE DEPT(
    DEPTNO int(2) PRIMARY KEY,
    DNAME VARCHAR(14) ,
    LOC VARCHAR(13) ) ; 
                        
INSERT INTO DEPT VALUES(10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES(30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES(40,'OPERATIONS','BOSTON'); 


 CREATE TABLE EMP
   (EMPNO int(4) primary key ,
    ENAME VARCHAR(10),
    JOB VARCHAR(9),
    MGR int(4),
    HIREDATE DATE,
    SAL decimal(7,2),
    COMM  decimal(7,2),
    DEPTNO int(2),
    foreign key (DEPTNO) references DEPT (DEPTNO)    
   );

 INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,'1981-4-2',2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,'1985-5-1',2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,'1981-6-9',2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,'1987-6-12',3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,'1987-6-13',1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,'1981-12-3', 950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,'1981-12-3' ,3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,'1982-1-23', 1300,NULL,10);

 CREATE TABLE BONUS
    (
    ENAME VARCHAR(10)    ,
    JOB VARCHAR(9)  ,
    SAL int,
    COMM int
    ) ;

 
CREATE TABLE SALGRADE
      ( GRADE int,
    LOSAL int,
    HISAL int );

INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);

表結構

dept

emp

 

 salgrade

bonus

=========================================

開始查詢練習(單表查詢)

1.查詢所有崗位名稱

select DISTINCT  JOB  from EMP ;

2.查詢所有年薪超過30000的員工信息 

select e.ENAME ,( e.SAL)*12 as totalsal from EMP e where (e.SAL)*12 > 30000;

3.查詢所有沒有獎金的員工信息 
select * from EMP where (COMM is NULL OR COMM = 0);

4.查詢所有有獎金的員工信息 

select * from EMP where (COMM is not NULL and comm !=0);

5.查詢所有薪水在2000-4000范圍內的員工信息 [not between]
select * from EMP where SAL BETWEEN 2000 AND 4000;

6.查詢所有部門編號是10或30的員工信息  [not in]
select * from EMP where DEPTNO IN (10,30);

select * from EMP where emp.DEPTNO=10 or emp.DEPTNO=30;

7.查詢姓名里面包含ALL的員工姓名 

select ENAME from EMP WHERE ENAME LIKE ‘%ALL%’;

8.查詢所有以”S”開頭的同學 
select * from EMP WHERE ENAME LIKE ‘S%’;

9.查詢第三個字母為A的員工姓名 (_為占位符號)
select ENAME from EMP WHERE ENAME LIKE '__A%';

10.查詢所有員工信息,按照部門降序排列,部門內按照薪水升序排列 
select * from EMP ORDER BY DEPTNO DESC,SAL ASC;

 

11.查詢雇員表中,姓名為SMITH的雇員,截止到今天共工作了多少周

SELECT ROUND (datediff(now(),HIREDATE)/7) AS  Weeks  FROM EMP WHERE ENAME = 'SMITH';

 

【mysql日期函數詳細參考:https://www.cnblogs.com/ggjucheng/p/3352280.html】

MySQL 日期、時間相減函數:datediff(date1,date2), timediff(time1,time2)

MySQL datediff(date1,date2):兩個日期相減 date1 - date2,返回天數。
select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); -- -7

MySQL timediff(time1,time2):兩個日期相減 time1 - time2,返回 time 差值。

select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08

注意:timediff(time1,time2) 函數的兩個參數類型必須相同。

MySQL 為日期減去一個時間間隔:date_sub()

 
mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);

+----------------------------------------------------------------+
| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 1997-12-30 22:58:59 |
+----------------------------------------------------------------+
 

MySQL 為日期增加一個時間間隔:date_add()

set @dt = now();

select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day
 MySQL 時間戳(timestamp)轉換、增、減函數:
 
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01

select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00

MySQL timestampadd() 函數類似於 date_add()。
select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12

select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7
 

mysql時區

convert_tz(dt,from_tz,to_tz)

select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00

時區轉換也可以通過 date_add, date_sub, timestampadd 來實現。

select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00

 

12.查詢‘SMITH’的領導姓名 
SELECT ENAME FROM EMP WHERE EMPNO = (SELECT MGR FROM EMP WHERE ENAME = ‘SMITH’);  推薦子查詢

select a.* from emp b,emp a where b.ENAME='SMITH'and b.MGR=a.EMPNO

 



13.查詢部門名稱是‘SALES’的員工信息 

select * from emp where emp.DEPTNO = (select d.DEPTNO from dept d where d.DNAME='SALES');

 

14.查詢公司中薪水最高的員工信息

select * from emp e where e.SAL=(select max(SAL) from emp);

 

15.查詢各部門的平均薪水及部門編號,要求只有員工姓名中包含 ‘A’才參與統計,只列出平均薪水>1500的,按照平均薪水降序排列 【group by 分組后的條件只能用 having】

select avg(e.sal),e.DEPTNO from emp e where e.ENAME like '%A%'group by e.DEPTNO having avg(e.sal)>1500 order by avg(e.sal) desc;

 

16.查詢各部門最高薪水的員工信息

create view vi as select max(emp.SAL) sal, emp.DEPTNO from emp group by emp.DEPTNO;
select f.* from emp f ,vi v where f.SAL= v.sal and f.DEPTNO = v.DEPTNO;

or 【in 雙向匹配】

select * from EMP where (deptno,SAL) in (select deptno,MAX(sal) from emp GROUP BY DEPTNO);

17.查詢薪水大於該部門平均薪水的員工信息 

 select * from emp f where f.sal > (select avg(e.sal)  from emp e where e.DEPTNO = f.DEPTNO  ) 

 

 

18.查詢薪水等級為4的員工信息

select * from emp e where e.sal between (select g.LOSAL from salgrade g where g.GRADE=4) and (select g.HISAL from salgrade g where g.GRADE=4);

 

19.查詢領導者是‘BLAKE’的員工信息

select * from emp e where e.MGR=(select f.EMPNO from emp f where f.ENAME='BLAKE');

 

20.查詢不是領導的員工信息 【ifnull() ,not exists】

select * from emp e1 where not EXISTS (select * from emp e2 where e2.mgr = e1.empno);

 

or

select * from emp e where e.EMPNO not in (select ifnull(f.mgr,0) from emp f );

 

IFNULL(expr1,expr2) 

如果expr1不是NULL,IFNULL()返回expr1,否則它返回expr2。IFNULL()返回一個數字或字符串值,取決於它被使用的上下文環境。 是有問題的,0不是null

 

 

21.查詢平均工資比10部門低的部門編號 

select  e.DEPTNO,avg(e.sal) from  emp e  group by  e.DEPTNO having avg(e.sal) < (select avg(f.sal) from emp f where f.deptno='10');

 

22.查詢在紐約工作的所有員工 

select * from emp e where e.DEPTNO = (select d.DEPTNO from dept d where d.LOC='NEW YORK')

 

23.查詢‘SALES’部門平均薪水的等級 

select s.GRADE from salgrade s where

(select avg(e.sal) avgsal from emp e where e.DEPTNO = (select d.DEPTNO from dept d where d.DNAME='SALES')) between s.LOSAL and s.HISAL;

 

24.查詢10號部門的員工在整個公司中所占的比例 【dual】

select (select count(*) from emp e where e.DEPTNO='10')/(select count(*) from emp) FROM dual

25.每頁顯示5條。 limit[m,n] m起始頁 n每頁顯示數
顯示第一頁內容:

select  EMP.* from emp where 1=1 limit 0,5

顯示第二頁的內容:

select * from emp where 1=1   limit 5,5;

 

26.查詢所有領導的信息:要求使用exists關鍵字【exists 和 in的區別】

select e.* from emp e  where e.EMPNO in (select f.MGR from emp f);

select * from emp e1 where EXISTS (select * from emp e2 where e2.mgr = e1.empno);

 

“外層查詢表小於子查詢表,則用exists,外層查詢表大於子查詢表,則用in,如果外層和子查詢表差不多,則愛用哪個用哪個。”

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM