-- 部門表
create table dept (
id int primary key primary key, -- 部門id
dname varchar(50), -- 部門名稱
loc varchar(50) -- 部門所在地
);
-- 新增4個部門
insert into dept(id,dname,loc) values
(10,'教研部','北京'),
(20,'學工部','上海'),
(30,'銷售部','廣州'),
(40,'財務部','深圳');
-- 職務表,職務名稱,職務描述
create table job (
id int primary key,
jname varchar(20),
description varchar(50)
);
-- 新增4個職務
insert into job (id, jname, description) values
(1, '董事長', '管理整個公司,接單'),
(2, '經理', '管理部門員工'),
(3, '銷售員', '向客人推銷產品'),
(4, '文員', '使用辦公軟體');
-- 員工表
create table emp (
id int primary key, -- 員工id
ename varchar(50), -- 員工姓名
job_id int, -- 職務id
mgr int , -- 上級領導
joindate date, -- 入職日期
salary decimal(7,2), -- 工資
bonus decimal(7,2), -- 獎金
dept_id int, -- 所在部門編號
constraint emp_jobid_ref_job_id_fk foreign key (job_id) references job (id),
constraint emp_deptid_ref_dept_id_fk foreign key (dept_id) references dept (id)
);
-- 新增員工
insert into emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) values
(1001,'孫悟空',4,1004,'2000-12-17','8000.00',null,20),
(1002,'盧俊義',3,1006,'2001-02-20','16000.00','3000.00',30),
(1003,'林沖',3,1006,'2001-02-22','12500.00','5000.00',30),
(1004,'唐僧',2,1009,'2001-04-02','29750.00',null,20),
(1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30),
(1006,'宋江',2,1009,'2001-05-01','28500.00',null,30),
(1007,'劉備',2,1009,'2001-09-01','24500.00',null,10),
(1008,'豬八戒',4,1004,'2007-04-19','30000.00',null,20),
(1009,'羅貫中',1,null,'2001-11-17','50000.00',null,10),
(1010,'吳用',3,1006,'2001-09-08','15000.00','0.00',30),
(1011,'沙僧',4,1004,'2007-05-23','11000.00',null,20),
(1012,'李逵',4,1006,'2001-12-03','9500.00',null,30),
(1013,'小白龍',4,1004,'2001-12-03','30000.00',null,20),
(1014,'關羽',4,1007,'2002-01-23','13000.00',null,10);
-- 工資等級表
create table salarygrade (
grade int primary key, -- 級別
losalary int, -- 最低工資
hisalary int -- 最高工資
);
-- 新增5個工資等級
insert into salarygrade(grade,losalary,hisalary) values
(1,7000,12000),
(2,12010,14000),
(3,14010,20000),
(4,20010,30000),
(5,30010,99990);
select
emp.`id`,
emp.`ename`,
emp.`salary`,
job.`jname`,
job.`description`
from
emp,
jobwhere emp.`job_id`=job.`id`;
select
emp.`id`,
emp.`ename`,
emp.`salary`,
job.`jname`,
job.`description`,
dept.`dname`,
dept.`loc`
from
emp,
job,
dept
where emp.`job_id`=job.`id` and emp.`dept_id`=dept.`id`;
select
emp.`ename`,
emp.`salary`,
salarygrade.`grade`
from
emp,
salarygrade
where emp.`salary` between salarygrade.`losalary` and salarygrade.`hisalary`;
select
emp.`ename`,
emp.`salary`,
job.`jname`,
job.`description`,
dept.`dname`,
dept.`loc`,
salarygrade.`grade`
from
emp,
job,
dept,
salarygrade
where emp.`job_id`=job.`id` and
emp.`dept_id`=dept.`id` and
emp.`salary` between salarygrade.`losalary` and salarygrade.`hisalary`;
select
dept.`id`,
dept.`dname`,
dept.`loc`,
total
from
dept,
(select
dept_id,
count(id) total
from
empgroup by dept_id) temp
where temp.dept_id=dept.`id`;
select
t1.`ename`,
t1.`mgr`,
t2.`id`,
t2.`ename`
from
emp t1
left join emp t2
on t1.`mgr`=t2.`id`;
MYSQL多表查詢筆記
1 等值連線查詢 查詢的結果為兩個表匹配到的資料 語法 select from 表1,表2 where 條件 and 條件 例子 select from 表1,表2,表3,where 表1.欄位1 表2.欄位1 and 表2.欄位2 表3.欄位2 ps where 後面支援多種運算子,進行條件處理 ...
MySQL筆記 MySQL多表查詢
主鍵 一張從表中某個字段引用主表中的主鍵,維護多表之間的關係 從表 使用別人資料的表,被主表約束 級聯兩個表,乙個表的關鍵字段進行修改,另乙個表也會隨之修改 constraint 外來鍵約束名 foreign key 外來鍵字段 references 主表名 字段 刪除外來鍵 alter table...
MySQL基礎篇 09 多表查詢之聯合查詢
select 語句1 union union選項 select 語句2 查詢同一張表但是需求不同 如查詢學生資訊,男生生高公升序,女生公升高降序 create table student id int primary key auto increment,number varchar 20 not ...