标签:
mysql多表查询
前提如下:
person表:
----------------------------------
id name dept_id salary
0001 wangda 101 1500
0002 wanger 103 3000
0003 wangsan 108 4000
0004 wangsi 109 2000
0005 wangwu 101 6000
0006 wangliu 102 7000
0007 wangqi [NULL] 4000
----------------------------------
dept表:
----------------------------------
dept_id dept_name
101 caiwu
102 zhenggong
103 wuzi
108 shangwu
109 renshi
110[NULL]
----------------------------------
salary_grade表:
----------------------------------
salary_grade lowest_sal highest_sal
A 0 999
B 1000 1999
C 2000 2999
D 3000 4999
E 5000 6999
F 7000 9999
----------------------------------
SELECT
p.name,
p.dept_id,
d.dept_name
FROM
person p,
dept d
WHERE
p.dept_id = d.dept_id;
name dept_id dept_name
wangda 101 caiwu
wangwu 101 caiwu
wangliu 102 zhenggong
wanger 103 wuzi
wangsan 108 shangwu
wangsi 109 renshi
SELECT
p.`name`,
p.dept_id,
d.dept_name,
p.salary,
s.salary_grade
FROM
person p,
dept d,
salary_grade s
WHERE
p.dept_id = d.dept_id
AND p.salary BETWEEN s.lowest_sal
AND s.highest_sal;
name dept_id dept_name salary salary_grade
wangda 101 caiwu 1500 B
wangsi 109 renshi 2000 C
wanger 103 wuzi 3000 D
wangsan 108 shangwu 4000 D
wangwu 101 caiwu 6000 E
wangliu 102 zhenggong 7000 F
SELECT
p. NAME,
d.dept_name
FROM
person p
LEFT JOIN dept d ON p.dept_id = d.dept_id;
NAME dept_name
wanger wuzi
wangsan shangwu
wangsi renshi
wangwu caiwu
wangliu zhenggong
wangqi [NULL]
SELECT
p. NAME,
d.dept_name
FROM
person p
RIGHT JOIN dept d ON p.dept_id = d.dept_id;
NAME dept_id dept_name
wangda 101 caiwu
wangwu 101 caiwu
wangliu 102 zhenggong
wanger 103 wuzi
wangsan 108 shangwu
wangsi 109 renshi
[NULL]110[NULL]
SELECT
*
FROM
person p
LEFT JOIN dept d ON p.dept_id = d.dept_id
UNION
SELECT
*
FROM
person p
RIGHT JOIN dept d ON p.dept_id = d.dept_id;
id name dept_id salary dept_id1 dept_name
0001 wangda 101 1500 101 caiwu
0002 wanger 103 3000 103 wuzi
0003 wangsan 108 4000 108 shangwu
0004 wangsi 109 2000 109 renshi
0005 wangwu 101 6000 101 caiwu
0006 wangliu 102 7000 102 zhenggong
0007 wangqi [NULL]4000[NULL][NULL]
[NULL][NULL][NULL][NULL]110[NULL]
SELECT
*
FROM
person
NATURAL JOIN dept;
dept_id id name salary dept_name
1010001 wangda 1500 caiwu
1010005 wangwu 6000 caiwu
1020006 wangliu 7000 zhenggong
1030002 wanger 3000 wuzi
1080003 wangsan 4000 shangwu
1090004 wangsi 2000 renshi
SELECT
*
FROM
person
JOIN dept USING (dept_id);
dept_id id name salary dept_name
1010001 wangda 1500 caiwu
1010005 wangwu 6000 caiwu
1020006 wangliu 7000 zhenggong
1030002 wanger 3000 wuzi
1080003 wangsan 4000 shangwu
1090004 wangsi 2000 renshi
id name dept_id salary manager_id
0001 wangda 101 1500 0005
0002 wanger 103 3000 0006
0003 wangsan 108 4000 0005
0004 wangsi 109 2000 0006
0005 wangwu 101 6000 0007
0006 wangliu 102 7000 0007
0007 wangqi [NULL]9000[NULL]
SELECT
e.id employee_id,
e. NAME employee,
m.id manager_id,
m.`name` manager
FROM
person e,
person m
WHERE
e.manager_id = m.id
AND lower(e. NAME)=‘wangsi‘;
employee_id employee manager_id manager
0004 wangsi 0006 wangliu
标签:
原文地址:http://www.cnblogs.com/rainyeagle/p/4599453.html