MySql基礎知識(2)
MySQL day02
1、關於查詢結果集的去重?
mysql> select distinct job from emp; // distinct關鍵字去除重複記錄。
+-----------+
| job |
+-----------+
| CLERK |
| SALESMAN |
| MANAGER |
| ANALYST |
| PRESIDENT |
+-----------+
mysql> select ename,distinct job from emp;
以上的sql語句是錯誤的。
記住:distinct只能出現在所有欄位的最前面。
mysql> select distinct deptno,job from emp;
+--------+-----------+
| deptno | job |
+--------+-----------+
| 20 | CLERK |
| 30 | SALESMAN |
| 20 | MANAGER |
| 30 | MANAGER |
| 10 | MANAGER |
| 20 | ANALYST |
| 10 | PRESIDENT |
| 30 | CLERK |
| 10 | CLERK |
+--------+-----------+
案例:統計崗位的數量?
select count(distinct job) from emp;
+---------------------+
| count(distinct job) |
+---------------------+
| 5 |
+---------------------+
2、連線查詢
2.1、什麼是連線查詢?
在實際開發中,大部分的情況下都不是從單表中查詢資料,一般都是多張表聯合查詢取出最終的結果。
在實際開發中,一般一個業務都會對應多張表,比如:學生和班級,起碼兩張表。
stuno stuname classno classname
-----------------------------------------------------------------------------------
1 zs 1 北京大興區亦莊經濟技術開發區第二中學高三1班
2 ls 1 北京大興區亦莊經濟技術開發區第二中學高三1班
...
學生和班級資訊儲存到一張表中,結果就像上面一樣,資料會存在大量的重複,導致資料的冗餘。
2.2、連線查詢的分類?
根據語法出現的年代來劃分的話,包括:
SQL92(一些老的DBA可能還在使用這種語法。DBA:DataBase Administrator,資料庫管理員)
SQL99(比較新的語法)
根據表的連線方式來劃分,包括:
內連線:
等值連線
非等值連線
自連線
外連線:
左外連線(左連線)
右外連線(右連線)
全連線(這個不講,很少用!)
2.3、在表的連線查詢方面有一種現象被稱為:笛卡爾積現象。(笛卡爾乘積現象)
案例:找出每一個員工的部門名稱,要求顯示員工名和部門名。
EMP表
+--------+--------+
| ename | deptno |
+--------+--------+
| SMITH | 20 |
| ALLEN | 30 |
| WARD | 30 |
| JONES | 20 |
| MARTIN | 30 |
| BLAKE | 30 |
| CLARK | 10 |
| SCOTT | 20 |
| KING | 10 |
| TURNER | 30 |
| ADAMS | 20 |
| JAMES | 30 |
| FORD | 20 |
| MILLER | 10 |
+--------+--------+
DEPT表
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
select ename,dname from emp,dept;
+--------+------------+
| ename | dname |
+--------+------------+
| SMITH | ACCOUNTING |
| SMITH | RESEARCH |
| SMITH | SALES |
| SMITH | OPERATIONS |
| ALLEN | ACCOUNTING |
| ALLEN | RESEARCH |
| ALLEN | SALES |
| ALLEN | OPERATIONS |
............
56 rows in set (0.00 sec)
笛卡爾積現象:當兩張表進行連線查詢的時候,沒有任何條件進行限制,最終的查詢結果條數是兩張表記錄條數的乘積。
關於表的別名:
select e.ename,d.dname from emp e,dept d;
表的別名有什麼好處?
第一:執行效率高。
第二:可讀性好。
2.4、怎麼避免笛卡爾積現象?當然是加條件進行過濾。
思考:避免了笛卡爾積現象,會減少記錄的匹配次數嗎?
不會,次數還是56次。只不過顯示的是有效記錄。
案例:找出每一個員工的部門名稱,要求顯示員工名和部門名。
select
e.ename,d.dname
from
emp e , dept d
where
e.deptno = d.deptno; //SQL92,以後不用。
+--------+------------+
| ename | dname |
+--------+------------+
| CLARK | ACCOUNTING |
| KING | ACCOUNTING |
| MILLER | ACCOUNTING |
| SMITH | RESEARCH |
| JONES | RESEARCH |
| SCOTT | RESEARCH |
| ADAMS | RESEARCH |
| FORD | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| MARTIN | SALES |
| BLAKE | SALES |
| TURNER | SALES |
| JAMES | SALES |
+--------+------------+
2.5、內連線之等值連線:最大特點是:條件是等量關係。
案例:查詢每個員工的部門名稱,要求顯示員工名和部門名。
SQL92:(太老,不用了)
select
e.ename,d.dname
from
emp e, dept d
where
e.deptno = d.deptno;
SQL99:(常用的)
select
e.ename,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno;
// inner可以省略的,帶著inner目的是可讀性好一些。
select
e.ename,d.dname
from
emp e
inner join
dept d
on
e.deptno = d.deptno;
語法:
...
A
join
B
on
連線條件
where
...
SQL99語法結構更清晰一些:表的連線條件和後來的where條件分離了。
+--------+------------+
| ename | dname |
+--------+------------+
| CLARK | ACCOUNTING |
| KING | ACCOUNTING |
| MILLER | ACCOUNTING |
| SMITH | RESEARCH |
| JONES | RESEARCH |
| SCOTT | RESEARCH |
| ADAMS | RESEARCH |
| FORD | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| MARTIN | SALES |
| BLAKE | SALES |
| TURNER | SALES |
| JAMES | SALES |
+--------+------------+
2.6、內連線之非等值連線:最大的特點是:連線條件中的關係是非等量關係。
案例:找出每個員工的工資等級,要求顯示員工名、工資、工資等級。
mysql> select ename,sal from emp; e
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
mysql> select * from salgrade; s
+-------+-------+-------+
| GRADE | LOSAL | HISAL |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
select
e.ename,e.sal,s.grade
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal;
// inner可以省略
select
e.ename,e.sal,s.grade
from
emp e
inner join
salgrade s
on
e.sal between s.losal and s.hisal;
+--------+---------+-------+
| ename | sal | grade |
+--------+---------+-------+
| SMITH | 800.00 | 1 |
| ALLEN | 1600.00 | 3 |
| WARD | 1250.00 | 2 |
| JONES | 2975.00 | 4 |
| MARTIN | 1250.00 | 2 |
| BLAKE | 2850.00 | 4 |
| CLARK | 2450.00 | 4 |
| SCOTT | 3000.00 | 4 |
| KING | 5000.00 | 5 |
| TURNER | 1500.00 | 3 |
| ADAMS | 1100.00 | 1 |
| JAMES | 950.00 | 1 |
| FORD | 3000.00 | 4 |
| MILLER | 1300.00 | 2 |
+--------+---------+-------+
2.7、自連線:最大的特點是:一張表看做兩張表。自己連線自己。
案例:找出每個員工的上級領導,要求顯示員工名和對應的領導名。
mysql> select empno,ename,mgr from emp;
emp a 員工表
+-------+--------+------+
| empno | ename | mgr |
+-------+--------+------+
| 7369 | SMITH | 7902 |
| 7499 | ALLEN | 7698 |
| 7521 | WARD | 7698 |
| 7566 | JONES | 7839 |
| 7654 | MARTIN | 7698 |
| 7698 | BLAKE | 7839 |
| 7782 | CLARK | 7839 |
| 7788 | SCOTT | 7566 |
| 7839 | KING | NULL |
| 7844 | TURNER | 7698 |
| 7876 | ADAMS | 7788 |
| 7900 | JAMES | 7698 |
| 7902 | FORD | 7566 |
| 7934 | MILLER | 7782 |
+-------+--------+------+
emp b 領導表
+-------+--------+
| empno | ename |
+-------+--------+
| 7566 | JONES |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7788 | SCOTT |
| 7839 | KING |
| 7902 | FORD |
+-------+--------+
員工的領導編號 = 領導的員工編號
select
a.ename as '員工名',b.ename as '領導名'
from
emp a
inner join
emp b
on
a.mgr = b.empno;
+--------+--------+
| 員工名 | 領導名 |
+--------+--------+
| SMITH | FORD |
| ALLEN | BLAKE |
| WARD | BLAKE |
| JONES | KING |
| MARTIN | BLAKE |
| BLAKE | KING |
| CLARK | KING |
| SCOTT | JONES |
| TURNER | BLAKE |
| ADAMS | SCOTT |
| JAMES | BLAKE |
| FORD | JONES |
| MILLER | CLARK |
+--------+--------+
2.8、外連線?
什麼是外連線,和內連線有什麼區別?
內連線:
假設A和B表進行連線,使用內連線的話,凡是A表和B表能夠匹配上的記錄查詢出來,這就是內連線。
AB兩張表沒有主副之分,兩張表是平等的。
外連線:
假設A和B表進行連線,使用外連線的話,AB兩張表中有一張表是主表,一張表是副表,主要查詢主表中
的資料,捎帶著查詢副表,當副表中的資料沒有和主表中的資料匹配上,副表自動模擬出NULL與之匹配。
外連線的分類?
左外連線(左連線):表示左邊的這張表是主表。
右外連線(右連線):表示右邊的這張表是主表。
左連線有右連線的寫法,右連線也會有對應的左連線的寫法。
案例:找出每個員工的上級領導?(所有員工必須全部查詢出來。)
emp a 員工表
+-------+--------+------+
| empno | ename | mgr |
+-------+--------+------+
| 7369 | SMITH | 7902 |
| 7499 | ALLEN | 7698 |
| 7521 | WARD | 7698 |
| 7566 | JONES | 7839 |
| 7654 | MARTIN | 7698 |
| 7698 | BLAKE | 7839 |
| 7782 | CLARK | 7839 |
| 7788 | SCOTT | 7566 |
| 7839 | KING | NULL |
| 7844 | TURNER | 7698 |
| 7876 | ADAMS | 7788 |
| 7900 | JAMES | 7698 |
| 7902 | FORD | 7566 |
| 7934 | MILLER | 7782 |
+-------+--------+------+
emp b 領導表
+-------+--------+
| empno | ename |
+-------+--------+
| 7566 | JONES |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7788 | SCOTT |
| 7839 | KING |
| 7902 | FORD |
+-------+--------+
內連線:
select
a.ename '員工', b.ename '領導'
from
emp a
join
emp b
on
a.mgr = b.empno;
外連線:(左外連線/左連線)
select
a.ename '員工', b.ename '領導'
from
emp a
left join
emp b
on
a.mgr = b.empno;
// outer是可以省略的。
select
a.ename '員工', b.ename '領導'
from
emp a
left outer join
emp b
on
a.mgr = b.empno;
外連線:(右外連線/右連線)
select
a.ename '員工', b.ename '領導'
from
emp b
right join
emp a
on
a.mgr = b.empno;
// outer可以省略。
select
a.ename '員工', b.ename '領導'
from
emp b
right outer join
emp a
on
a.mgr = b.empno;
+--------+-------+
| 員工 | 領導 |
+--------+-------+
| SMITH | FORD |
| ALLEN | BLAKE |
| WARD | BLAKE |
| JONES | KING |
| MARTIN | BLAKE |
| BLAKE | KING |
| CLARK | KING |
| SCOTT | JONES |
| KING | NULL |
| TURNER | BLAKE |
| ADAMS | SCOTT |
| JAMES | BLAKE |
| FORD | JONES |
| MILLER | CLARK |
+--------+-------+
外連線最重要的特點是:主表的資料無條件的全部查詢出來。
案例:找出哪個部門沒有員工?
EMP表
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
DEPT
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
select
d.*
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
e.empno is null;
+--------+------------+--------+
| DEPTNO | DNAME | LOC |
+--------+------------+--------+
| 40 | OPERATIONS | BOSTON |
+--------+------------+--------+
2.9、三張表怎麼連線查詢?
案例:找出每一個員工的部門名稱以及工資等級。
EMP e
+-------+--------+---------+--------+
| empno | ename | sal | deptno |
+-------+--------+---------+--------+
| 7369 | SMITH | 800.00 | 20 |
| 7499 | ALLEN | 1600.00 | 30 |
| 7521 | WARD | 1250.00 | 30 |
| 7566 | JONES | 2975.00 | 20 |
| 7654 | MARTIN | 1250.00 | 30 |
| 7698 | BLAKE | 2850.00 | 30 |
| 7782 | CLARK | 2450.00 | 10 |
| 7788 | SCOTT | 3000.00 | 20 |
| 7839 | KING | 5000.00 | 10 |
| 7844 | TURNER | 1500.00 | 30 |
| 7876 | ADAMS | 1100.00 | 20 |
| 7900 | JAMES | 950.00 | 30 |
| 7902 | FORD | 3000.00 | 20 |
| 7934 | MILLER | 1300.00 | 10 |
+-------+--------+---------+--------+
DEPT d
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
SALGRADE s
+-------+-------+-------+
| GRADE | LOSAL | HISAL |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
注意,解釋一下:
....
A
join
B
join
C
on
...
表示:A表和B表先進行表連線,連線之後A表繼續和C表進行連線。
select
e.ename,d.dname,s.grade
from
emp e
join
dept d
on
e.deptno = d.deptno
join
salgrade s
on
e.sal between s.losal and s.hisal;
+--------+------------+-------+
| ename | dname | grade |
+--------+------------+-------+
| SMITH | RESEARCH | 1 |
| ALLEN | SALES | 3 |
| WARD | SALES | 2 |
| JONES | RESEARCH | 4 |
| MARTIN | SALES | 2 |
| BLAKE | SALES | 4 |
| CLARK | ACCOUNTING | 4 |
| SCOTT | RESEARCH | 4 |
| KING | ACCOUNTING | 5 |
| TURNER | SALES | 3 |
| ADAMS | RESEARCH | 1 |
| JAMES | SALES | 1 |
| FORD | RESEARCH | 4 |
| MILLER | ACCOUNTING | 2 |
+--------+------------+-------+
案例:找出每一個員工的部門名稱、工資等級、以及上級領導。
select
e.ename '員工',d.dname,s.grade,e1.ename '領導'
from
emp e
join
dept d
on
e.deptno = d.deptno
join
salgrade s
on
e.sal between s.losal and s.hisal
left join
emp e1
on
e.mgr = e1.empno;
+--------+------------+-------+-------+
| 員工 | dname | grade | 領導 |
+--------+------------+-------+-------+
| SMITH | RESEARCH | 1 | FORD |
| ALLEN | SALES | 3 | BLAKE |
| WARD | SALES | 2 | BLAKE |
| JONES | RESEARCH | 4 | KING |
| MARTIN | SALES | 2 | BLAKE |
| BLAKE | SALES | 4 | KING |
| CLARK | ACCOUNTING | 4 | KING |
| SCOTT | RESEARCH | 4 | JONES |
| KING | ACCOUNTING | 5 | NULL |
| TURNER | SALES | 3 | BLAKE |
| ADAMS | RESEARCH | 1 | SCOTT |
| JAMES | SALES | 1 | BLAKE |
| FORD | RESEARCH | 4 | JONES |
| MILLER | ACCOUNTING | 2 | CLARK |
+--------+------------+-------+-------+
3、子查詢
3.1、什麼是子查詢?子查詢都可以出現在哪裡?
select語句當中巢狀select語句,被巢狀的select語句是子查詢。
子查詢可以出現在哪裡?
select
..(select).
from
..(select).
where
..(select).
3.2、where子句中使用子查詢
案例:找出高於平均薪資的員工資訊。
select * from emp where sal > avg(sal); //錯誤的寫法,where後面不能直接使用分組函式。
第一步:找出平均薪資
select avg(sal) from emp;
+-------------+
| avg(sal) |
+-------------+
| 2073.214286 |
+-------------+
第二步:where過濾
select * from emp where sal > 2073.214286;
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+-----------+------+------------+---------+------+--------+
第一步和第二步合併:
select * from emp where sal > (select avg(sal) from emp);
3.3、from後面巢狀子查詢
案例:找出每個部門平均薪水的等級。
第一步:找出每個部門平均薪水(按照部門編號分組,求sal的平均值)
select deptno,avg(sal) as avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
第二步:將以上的查詢結果當做臨時表t,讓t表和salgrade s表連線,條件是:t.avgsal between s.losal and s.hisal
select
t.*,s.grade
from
(select deptno,avg(sal) as avgsal from emp group by deptno) t
join
salgrade s
on
t.avgsal between s.losal and s.hisal;
+--------+-------------+-------+
| deptno | avgsal | grade |
+--------+-------------+-------+
| 30 | 1566.666667 | 3 |
| 10 | 2916.666667 | 4 |
| 20 | 2175.000000 | 4 |
+--------+-------------+-------+
案例:找出每個部門平均的薪水等級。
第一步:找出每個員工的薪水等級。
select e.ename,e.sal,e.deptno,s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal;
+--------+---------+--------+-------+
| ename | sal | deptno | grade |
+--------+---------+--------+-------+
| SMITH | 800.00 | 20 | 1 |
| ALLEN | 1600.00 | 30 | 3 |
| WARD | 1250.00 | 30 | 2 |
| JONES | 2975.00 | 20 | 4 |
| MARTIN | 1250.00 | 30 | 2 |
| BLAKE | 2850.00 | 30 | 4 |
| CLARK | 2450.00 | 10 | 4 |
| SCOTT | 3000.00 | 20 | 4 |
| KING | 5000.00 | 10 | 5 |
| TURNER | 1500.00 | 30 | 3 |
| ADAMS | 1100.00 | 20 | 1 |
| JAMES | 950.00 | 30 | 1 |
| FORD | 3000.00 | 20 | 4 |
| MILLER | 1300.00 | 10 | 2 |
+--------+---------+--------+-------+
第二步:基於以上結果,繼續按照deptno分組,求grade平均值。
select
e.deptno,avg(s.grade)
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal
group by
e.deptno;
+--------+--------------+
| deptno | avg(s.grade) |
+--------+--------------+
| 10 | 3.6667 |
| 20 | 2.8000 |
| 30 | 2.5000 |
+--------+--------------+
3.4、在select後面巢狀子查詢。
案例:找出每個員工所在的部門名稱,要求顯示員工名和部門名。
select
e.ename,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno;
select
e.ename,(select d.dname from dept d where e.deptno = d.deptno) as dname
from
emp e;
+--------+------------+
| ename | dname |
+--------+------------+
| SMITH | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| JONES | RESEARCH |
| MARTIN | SALES |
| BLAKE | SALES |
| CLARK | ACCOUNTING |
| SCOTT | RESEARCH |
| KING | ACCOUNTING |
| TURNER | SALES |
| ADAMS | RESEARCH |
| JAMES | SALES |
| FORD | RESEARCH |
| MILLER | ACCOUNTING |
+--------+------------+
4、union (可以將查詢結果集相加)
案例:找出工作崗位是SALESMAN和MANAGER的員工?
第一種:select ename,job from emp where job = 'MANAGER' or job = 'SALESMAN';
第二種:select ename,job from emp where job in('MANAGER','SALESMAN');
+--------+----------+
| ename | job |
+--------+----------+
| ALLEN | SALESMAN |
| WARD | SALESMAN |
| JONES | MANAGER |
| MARTIN | SALESMAN |
| BLAKE | MANAGER |
| CLARK | MANAGER |
| TURNER | SALESMAN |
+--------+----------+
第三種:union
select ename,job from emp where job = 'MANAGER'
union
select ename,job from emp where job = 'SALESMAN';
+--------+----------+
| ename | job |
+--------+----------+
| JONES | MANAGER |
| BLAKE | MANAGER |
| CLARK | MANAGER |
| ALLEN | SALESMAN |
| WARD | SALESMAN |
| MARTIN | SALESMAN |
| TURNER | SALESMAN |
+--------+----------+
兩張不相干的表中的資料拼接在一起顯示?
select ename from emp
union
select dname from dept;
+------------+
| ename |
+------------+
| SMITH |
| ALLEN |
| WARD |
| JONES |
| MARTIN |
| BLAKE |
| CLARK |
| SCOTT |
| KING |
| TURNER |
| ADAMS |
| JAMES |
| FORD |
| MILLER |
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
mysql> select ename,sal from emp
-> union
-> select dname from dept;
ERROR 1222 (21000): The used SELECT statements have a different number of columns
5、limit (重點中的重點,以後分頁查詢全靠它了。)
5.1、limit是mysql特有的,其他資料庫中沒有,不通用。(Oracle中有一個相同的機制,叫做rownum)
5.2、limit取結果集中的部分資料,這時它的作用。
5.3、語法機制:
limit startIndex, length
startIndex表示起始位置,從0開始,0表示第一條資料。
length表示取幾個
案例:取出工資前5名的員工(思路:降序取前5個)
select ename,sal from emp order by sal desc;
取前5個:
select ename,sal from emp order by sal desc limit 0, 5;
select ename,sal from emp order by sal desc limit 5;
5.4、limit是sql語句最後執行的一個環節:
select 5
...
from 1
...
where 2
...
group by 3
...
having 4
...
order by 6
...
limit 7
...;
5.5、案例:找出工資排名在第4到第9名的員工?
select ename,sal from emp order by sal desc limit 3,6;
+--------+---------+
| ename | sal |
+--------+---------+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
+--------+---------+
5.6、通用的標準分頁sql?
每頁顯示3條記錄:
第1頁:0, 3
第2頁:3, 3
第3頁:6, 3
第4頁:9, 3
第5頁:12, 3
每頁顯示pageSize條記錄:
第pageNo頁:(pageNo - 1) * pageSize, pageSize
pageSize是什麼?是每頁顯示多少條記錄
pageNo是什麼?顯示第幾頁
java程式碼{
int pageNo = 2; // 頁碼是2
int pageSize = 10; // 每頁顯示10條
limit (pageNo - 1) * pageSize, pageSize
}
6、建立表:
建表語句的語法格式:
create table 表名(
欄位名1 資料型別,
欄位名2 資料型別,
欄位名3 資料型別,
....
);
關於MySQL當中欄位的資料型別?以下只說常見的
int 整數型(java中的int)
bigint 長整型(java中的long)
float 浮點型(java中的float double)
char 定長字串(String)
varchar 可變長字串(StringBuffer/StringBuilder)
date 日期型別 (對應Java中的java.sql.Date型別)
BLOB 二進位制大物件(儲存圖片、視訊等流媒體資訊) Binary Large OBject (對應java中的Object)
CLOB 字元大物件(儲存較大文字,比如,可以儲存4G的字串。) Character Large OBject(對應java中的Object)
......
char和varchar怎麼選擇?
在實際的開發中,當某個欄位中的資料長度不發生改變的時候,是定長的,例如:性別、生日等都是採用char。
當一個欄位的資料長度不確定,例如:簡介、姓名等都是採用varchar。
BLOB和CLOB型別的使用?
電影表: t_movie
id(int) name(varchar) playtime(date/char) haibao(BLOB) history(CLOB)
----------------------------------------------------------------------------------------
1 蜘蛛俠
2
3
表名在資料庫當中一般建議以:t_或者tbl_開始。
建立學生表:
學生資訊包括:
學號、姓名、性別、班級編號、生日
學號:bigint
姓名:varchar
性別:char
班級編號:int
生日:char
create table t_student(
no bigint,
name varchar(255),
sex char(1),
classno varchar(255),
birth char(10)
);
7、insert語句插入資料
語法格式:
insert into 表名(欄位名1,欄位名2,欄位名3,....) values(值1,值2,值3,....)
要求:欄位的數量和值的數量相同,並且資料型別要對應相同。
insert into t_student(no,name,sex,classno,birth) values(1,'zhangsan','1','gaosan1ban');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
insert into t_student(no,name,sex,classno,birth) values(1,'zhangsan','1','gaosan1ban', '1950-10-12');
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| 1 | zhangsan | 1 | gaosan1ban | 1950-10-12 |
+------+----------+------+------------+------------+
insert into t_student(name,sex,classno,birth,no) values('lisi','1','gaosan1ban', '1950-10-12',2);
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| 1 | zhangsan | 1 | gaosan1ban | 1950-10-12 |
| 2 | lisi | 1 | gaosan1ban | 1950-10-12 |
+------+----------+------+------------+------------+
insert into t_student(name) values('wangwu'); // 除name欄位之外,剩下的所有欄位自動插入NULL。
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| 1 | zhangsan | 1 | gaosan1ban | 1950-10-12 |
| 2 | lisi | 1 | gaosan1ban | 1950-10-12 |
| NULL | wangwu | NULL | NULL | NULL |
+------+----------+------+------------+------------+
insert into t_student(no) values(3);
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| 1 | zhangsan | 1 | gaosan1ban | 1950-10-12 |
| 2 | lisi | 1 | gaosan1ban | 1950-10-12 |
| NULL | wangwu | NULL | NULL | NULL |
| 3 | NULL | NULL | NULL | NULL |
+------+----------+------+------------+------------+
drop table if exists t_student; // 當這個表存在的話刪除。
create table t_student(
no bigint,
name varchar(255),
sex char(1) default 1,
classno varchar(255),
birth char(10)
);
insert into t_student(name) values('zhangsan');
mysql> select * from t_student;
+------+----------+------+---------+-------+
| no | name | sex | classno | birth |
+------+----------+------+---------+-------+
| NULL | zhangsan | 1 | NULL | NULL |
+------+----------+------+---------+-------+
需要注意的地方:
當一條insert語句執行成功之後,表格當中必然會多一行記錄。
即使多的這一行記錄當中某些欄位是NULL,後期也沒有辦法在執行
insert語句插入資料了,只能使用update進行更新。
// 欄位可以省略不寫,但是後面的value對數量和順序都有要求。
insert into t_student values(1,'jack','0','gaosan2ban','1986-10-23');
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| NULL | zhangsan | 1 | NULL | NULL |
| 1 | jack | 0 | gaosan2ban | 1986-10-23 |
+------+----------+------+------------+------------+
insert into t_student values(1,'jack','0','gaosan2ban');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
// 一次插入多行資料
insert into t_student
(no,name,sex,classno,birth)
values
(3,'rose','1','gaosi2ban','1952-12-14'),(4,'laotie','1','gaosi2ban','1955-12-14');
mysql> select * from t_student;
+------+----------+------+------------+------------+
| no | name | sex | classno | birth |
+------+----------+------+------------+------------+
| NULL | zhangsan | 1 | NULL | NULL |
| 1 | jack | 0 | gaosan2ban | 1986-10-23 |
| 3 | rose | 1 | gaosi2ban | 1952-12-14 |
| 4 | laotie | 1 | gaosi2ban | 1955-12-14 |
+------+----------+------+------------+------------+
8、表的複製
語法:
create table 表名 as select語句;
將查詢結果當做表建立出來。
9、將查詢結果插入到一張表中?
mysql> insert into dept1 select * from dept;
mysql> select * from dept1;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
10、修改資料:update
語法格式:
update 表名 set 欄位名1=值1,欄位名2=值2... where 條件;
注意:沒有條件整張表資料全部更新。
案例:將部門10的LOC修改為SHANGHAI,將部門名稱修改為RENSHIBU
update dept1 set loc = 'SHANGHAI', dname = 'RENSHIBU' where deptno = 10;
mysql> select * from dept1;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | RENSHIBU | SHANGHAI |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 10 | RENSHIBU | SHANGHAI |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
更新所有記錄
update dept1 set loc = 'x', dname = 'y';
mysql> select * from dept1;
+--------+-------+------+
| DEPTNO | DNAME | LOC |
+--------+-------+------+
| 10 | y | x |
| 20 | y | x |
| 30 | y | x |
| 40 | y | x |
| 10 | y | x |
| 20 | y | x |
| 30 | y | x |
| 40 | y | x |
+--------+-------+------+
11、刪除資料?
語法格式:
delete from 表名 where 條件;
注意:沒有條件全部刪除。
刪除10部門資料?
delete from dept1 where deptno = 10;
刪除所有記錄?
delete from dept1;
怎麼刪除大表中的資料?(重點)
truncate table 表名; // 表被截斷,不可回滾。永久丟失。
刪除表?
drop table 表名; // 這個通用。
drop table if exists 表名; // oracle不支援這種寫法。
12、對於表結構的修改,這裡不講了,大家使用工具完成即可,因為在實際開發中表一旦
設計好之後,對錶結構的修改是很少的,修改表結構就是對之前的設計進行了否定,即使
需要修改表結構,我們也可以直接使用工具操作。修改表結構的語句不會出現在Java程式碼當中。
出現在java程式碼當中的sql包括:insert delete update select(這些都是表中的資料操作。)
增刪改查有一個術語:CRUD操作
Create(增) Retrieve(檢索) Update(修改) Delete(刪除)
13、約束(Constraint)
13.1、什麼是約束?常見的約束有哪些呢?
在建立表的時候,可以給表的欄位新增相應的約束,新增約束的目的是為了保證表中資料的
合法性、有效性、完整性。
常見的約束有哪些呢?
非空約束(not null):約束的欄位不能為NULL
唯一約束(unique):約束的欄位不能重複
主鍵約束(primary key):約束的欄位既不能為NULL,也不能重複(簡稱PK)
外來鍵約束(foreign key):...(簡稱FK)
檢查約束(check):注意Oracle資料庫有check約束,但是mysql沒有,目前mysql不支援該約束。
13.2、非空約束 not null
drop table if exists t_user;
create table t_user(
id int,
username varchar(255) not null,
password varchar(255)
);
insert into t_user(id,password) values(1,'123');
ERROR 1364 (HY000): Field 'username' doesn't have a default value
insert into t_user(id,username,password) values(1,'lisi','123');
相關文章
- MySQL基礎知識(5)MySql
- MySQL基礎知識(6)MySql
- MySQL基礎知識(7)MySql
- MySQL基礎知識(8)MySql
- MySQL指南之基礎知識MySql
- MySQL基礎知識分享(二)MySql
- MySQL基礎知識分享(一)MySql
- Go 基礎教程--2-基礎知識Go
- OBD2基礎知識
- GO基礎知識分享2Go
- MySQL基礎知識小結(一)MySql
- 【編測編學】MySQL資料庫基礎知識2MySql資料庫
- Python知識體系-Python2基礎知識Python
- JS基礎知識總結(2)JS
- 嵌入式基礎(2)---硬體基礎知識
- MySQL-13.事務基礎知識MySql
- 深入mysql基礎知識的詳解MySql
- 第二章 基礎知識(2)
- 2020-2-20-nodejs基礎知識NodeJS
- yii2 基礎知識總結
- 基礎的python知識2 (變數)Python變數
- MySQL 基礎知識梳理學習(四)—-GTIDMySql
- 【學習】MySQL基礎知識要點-001MySql
- 關於mysql基礎知識的介紹MySql
- 滲透測試基礎知識----MySQL 配置MySql
- 基礎知識
- 資訊處理技術基礎知識(2.4多媒體基礎知識 )--第2章
- MySQL基礎知識(一)-超詳細MySQL安裝教程MySql
- mysql資料庫sql語句基礎知識MySql資料庫
- MySQL基礎知識點串講PART01MySql
- MySQL基礎知識點串講PART03MySql
- MySQL?資料庫的基礎知識撩磅MySql資料庫
- 全面的MySQL基礎運維知識點(一)MySql運維
- 全面的MySQL基礎運維知識點(三)MySql運維
- 全面的MySQL基礎運維知識點(二)MySql運維
- MySQL 資料庫基礎知識點複習MySql資料庫
- MySQL資料庫基礎知識及優化MySql資料庫優化
- mysql資料庫學習基礎知識整理MySql資料庫