目錄python
#建表 create table department( id int, name varchar(20) ); create table staff( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int ); #插入數據 insert into department values (200,'挖礦小分隊'), (201,'人力資源'), (202,'銷售'), (203,'運營'); insert into staff(name,sex,age,dep_id) values ('程咬金','male',38,200), ('露娜','female',26,201), ('李白','male',38,201), ('王昭君','female',28,202), ('典韋','male',118,200), ('小喬','female',16,204) ; #查看錶結構和數據 mysql> desc department; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.06 sec) mysql> desc staff; +--------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | sex | enum('male','female') | NO | | male | | | age | int(11) | YES | | NULL | | | dep_id | int(11) | YES | | NULL | | +--------+-----------------------+------+-----+---------+----------------+ 5 rows in set (0.04 sec) #表department與staff mysql> select * from department; +------+-----------------+ | id | name | +------+-----------------+ | 200 | 挖礦小分隊 | | 201 | 人力資源 | | 202 | 銷售 | | 203 | 運營 | +------+-----------------+ 4 rows in set (0.00 sec) mysql> select * from staff; +----+-----------+--------+------+--------+ | id | name | sex | age | dep_id | +----+-----------+--------+------+--------+ | 1 | 程咬金 | male | 38 | 200 | | 2 | 露娜 | female | 26 | 201 | | 3 | 李白 | male | 38 | 201 | | 4 | 王昭君 | female | 28 | 202 | | 5 | 典韋 | male | 118 | 200 | | 6 | 小喬 | female | 16 | 204 | +----+-----------+--------+------+--------+ 6 rows in set (0.00 sec)
小結: 找兩張表共有的部分,利用條件從笛卡爾積結果中篩選出了正確的結果mysql
select * from 表1 left join 表2 on 條件((表1.字段=表2.字段)) union select * from 表1 right join 表2 on 條件((表1.字段=表2.字段));
#全外鏈接:在內鏈接的基礎上增長左邊有右邊沒有的和右邊有左邊沒有的結果 #注意:mysql不支持全外鏈接 full join #強調:mysql能夠使用此種方式間接實現全外鏈接 #注意 union與union all的區別:union會去掉相同的紀錄
1.找挖礦小分隊的全部員工的信息 #答: mysql> select * from staff inner join department on department.id=staff.dep_id where department.name='挖礦小分隊'; +----+-----------+------+------+--------+------+-----------------+ | id | name | sex | age | dep_id | id | name | +----+-----------+------+------+--------+------+-----------------+ | 1 | 程咬金 | male | 38 | 200 | 200 | 挖礦小分隊 | | 5 | 典韋 | male | 118 | 200 | 200 | 挖礦小分隊 | +----+-----------+------+------+--------+------+-----------------+ 2 rows in set (0.00 sec) 2.查找人力資源全部的員工名字 #答: (名字太長能夠起別名) mysql> select staff.name from staff inner join department as dep on dep.id=staff.dep_id where dep.name='人力資源'; +--------+ | name | +--------+ | 露娜 | | 李白 | +--------+ 2 rows in set (0.00 sec) 3.找出年齡大於38的員工的姓名,及其所在的部門名稱 #答: mysql> select staff.name,dep.name from staff inner join department as dep on dep.id=staff.dep_id where age>38; +--------+-----------------+ | name | name | +--------+-----------------+ | 典韋 | 挖礦小分隊 | +--------+-----------------+ 1 row in set (0.00 sec) 4.之內鏈接的方式查詢 staff 和 department表,而且以age字段的升序方式顯示 答: mysql> select * from staff inner join department as dep on dep.id=staff.dep_id order by age; +----+-----------+--------+------+--------+------+-----------------+ | id | name | sex | age | dep_id | id | name | +----+-----------+--------+------+--------+------+-----------------+ | 2 | 露娜 | female | 26 | 201 | 201 | 人力資源 | | 4 | 王昭君 | female | 28 | 202 | 202 | 銷售 | | 1 | 程咬金 | male | 38 | 200 | 200 | 挖礦小分隊 | | 3 | 李白 | male | 38 | 201 | 201 | 人力資源 | | 5 | 典韋 | male | 118 | 200 | 200 | 挖礦小分隊 | +----+-----------+--------+------+--------+------+-----------------+ 5 rows in set (0.05 sec) 5.找到部門爲 挖礦小分隊 和 人力資源 的全部員工的名字 #答: mysql> select staff.name from staff inner join department as dep on dep.id = staff.dep_id where dep.name in ('挖礦小分隊','人力資源'); +-----------+ | name | +-----------+ | 程咬金 | | 露娜 | | 李白 | | 典韋 | +-----------+ 4 rows in set (0.00 sec)
#1:子查詢是將一個查詢語句嵌套在另外一個查詢語句中。 #2:內層查詢語句的查詢結果,能夠爲外層查詢語句提供查詢條件。 #3:子查詢中能夠包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字 #4:還能夠包含比較運算符:= 、 !=、> 、<等 #5: 多用連表查,由於連表查詢比子查詢效率高 1.用子查詢 找到部門是銷售的全部員工的姓名 #解題思路 ①先找department表部門爲銷售的部門的id mysql> select id from department where name = '銷售'; +------+ | id | +------+ | 202 | +------+ 1 row in set (0.00 sec) ②再找staff表中部門dep_id = 202 mysql> select name from staff where dep_id = 202; +-----------+ | name | +-----------+ | 王昭君 | +-----------+ 1 row in set (0.00 sec) ③字表查 mysql> select name from staff where dep_id =(select id from department where name = '銷售'); +-----------+ | name | +-----------+ | 王昭君 | +-----------+ 1 row in set (0.00 sec) 2.用子查詢 找到部門爲 銷售 和 人力資源 的全部員工的名字 ①先找department表部門爲銷售和人力資源的部門的id mysql> select id from department where name = '銷售' or name = '人力資源'; +------+ | id | +------+ | 201 | | 202 | +------+ 2 rows in set (0.00 sec) ②子查詢 mysql> select name from staff where dep_id in (select id from department where name = '銷售' or name = '人力資源'); +-----------+ | name | +-----------+ | 露娜 | | 李白 | | 王昭君 | +-----------+ 3 rows in set (0.00 sec)
1. 帶IN關鍵字的子查詢 ①查詢平均年齡在28歲以上的部門名 select id,name from department where id in (select dep_id from staff group by dep_id having avg(age) > 28); #結果 +------+-----------------+ | id | name | +------+-----------------+ | 200 | 挖礦小分隊 | | 201 | 人力資源 | +------+-----------------+ 2 rows in set (0.00 sec) ②查看部門是挖礦小分隊員工姓名 select name from staff where dep_id in (select id from department where name='挖礦小分隊'); #結果 +-----------+ | name | +-----------+ | 程咬金 | | 典韋 | +-----------+ 2 rows in set (0.00 sec) ③查看不足1人的部門名(子查詢獲得的是有人的部門id) select name from department where id not in (select distinct dep_id from staff); #結果 +--------+ | name | +--------+ | 運營 | +--------+ 1 row in set (0.02 sec) 2. 帶比較運算符的子查詢 #比較運算符:=、!=、>、>=、<、<=、<> ①查詢大於全部人平均年齡的員工名與年齡 mysql> select name,age from staff where age > (select avg(age) from staff); +--------+------+ | name | age | +--------+------+ | 典韋 | 118 | +--------+------+ 1 row in set (0.00 sec) ②查詢大於部門內平均年齡的員工名、年齡 select t1.name,t1.age from staff t1 inner join (select dep_id,avg(age) avg_age from staff group by dep_id) t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age; #結果 +--------+------+ | name | age | +--------+------+ | 李白 | 38 | | 典韋 | 118 | +--------+------+ 2 rows in set (0.04 sec) 3. 帶EXISTS關鍵字的子查詢 EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。 而是返回一個真假值。True或False 當返回True時,外層查詢語句將進行查詢;當返回值爲False時,外層查詢語句不進行查詢 #department表中存在dept_id=203,Ture select * from staff where exists (select id from department where id=200); #結果 +----+-----------+--------+------+--------+ | id | name | sex | age | dep_id | +----+-----------+--------+------+--------+ | 1 | 程咬金 | male | 38 | 200 | | 2 | 露娜 | female | 26 | 201 | | 3 | 李白 | male | 38 | 201 | | 4 | 王昭君 | female | 28 | 202 | | 5 | 典韋 | male | 118 | 200 | | 6 | 小喬 | female | 16 | 204 | +----+-----------+--------+------+--------+ 6 rows in set (0.00 sec) #department表中存在dept_id=205,False mysql> select * from staff where exists (select id from department where id=204); Empty set (0.00 sec)