MySQL (8.0以下版本) : SELECT WHERE Multiple column and group by max level
MySQL (version lower than 8.0) : SELECT WHERE Multiple column and group by max level
我的数据库中有 2 个 table。
我部门的 - table 名员工。这个 table 是集中存储的。我无权在此table
中进行管理
emp_id
name
job postion
dept_1
dept_2
dept_3
000010
emp1 name1
Director
CEO
Human resource
000012
emp2 name2
employee
CEO
Human resource
000013
emp2 name2
Director
CEO
Human resource
Recruitment
000014
emp2 name2
employee
CEO
Human resource
Recruitment
000015
emp2 name2
employee
CEO
Human resource
Recruitment
..
..
..
..
..
..
..
..
..
..
000200
emp2 name2
Head Director
CEO
- 部门table。这是我为展示我的部门而创建的 table。
dept_id
dept_level
dept_name
dept_order
1
1
CEO
1
2
2
Human Resource
2
2
3
Recruitment
3
2
3
Training
4
2
2
Sale
5
2
3
Sale planning
6
2
3
Sale marketing
7
我想知道员工属于哪个部门?
我尝试使用命令:
SELECT *
FROM table1
INNER table2 ON (table2.dept_name = table1.dept_1 OR table2.dept_name = table1.dept_2 OR table2.dept_name = table1.dept_3)
但这不是我想要的答案
我要
emp_id
name
job postion
dept_level
dept_name
dept_order
000010
emp1 name1
Director
2
Human Resource
2
000012
emp2 name2
employee
2
Human Resource
2
000013
emp2 name2
Director
3
Recruitment
3
000200
emp2 name2
Head Director
1
CEO
1
我该怎么办?
P.S。对不起我的英语
WITH cte AS ( SELECT *, MAX(d.dept_level) OVER (PARTITION BY e.emp_id) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3) )
SELECT *
FROM cte
WHERE dept_level = maxlevel
ORDER BY emp_id
对于 MySQL 5.7 / MariaDB 10.1 使用
SELECT *
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
JOIN ( SELECT e.emp_id,
MAX(d.dept_level) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
GROUP BY e.emp_id ) ed USING (emp_id)
WHERE d.dept_level = ed.maxlevel
ORDER BY emp_id
我的数据库中有 2 个 table。
-
我部门的
- table 名员工。这个 table 是集中存储的。我无权在此table 中进行管理
emp_id | name | job postion | dept_1 | dept_2 | dept_3 |
---|---|---|---|---|---|
000010 | emp1 name1 | Director | CEO | Human resource | |
000012 | emp2 name2 | employee | CEO | Human resource | |
000013 | emp2 name2 | Director | CEO | Human resource | Recruitment |
000014 | emp2 name2 | employee | CEO | Human resource | Recruitment |
000015 | emp2 name2 | employee | CEO | Human resource | Recruitment |
.. | .. | .. | .. | .. | |
.. | .. | .. | .. | .. | |
000200 | emp2 name2 | Head Director | CEO |
- 部门table。这是我为展示我的部门而创建的 table。
dept_id | dept_level | dept_name | dept_order |
---|---|---|---|
1 | 1 | CEO | 1 |
2 | 2 | Human Resource | 2 |
2 | 3 | Recruitment | 3 |
2 | 3 | Training | 4 |
2 | 2 | Sale | 5 |
2 | 3 | Sale planning | 6 |
2 | 3 | Sale marketing | 7 |
我想知道员工属于哪个部门?
我尝试使用命令:
SELECT *
FROM table1
INNER table2 ON (table2.dept_name = table1.dept_1 OR table2.dept_name = table1.dept_2 OR table2.dept_name = table1.dept_3)
但这不是我想要的答案
我要
emp_id | name | job postion | dept_level | dept_name | dept_order |
---|---|---|---|---|---|
000010 | emp1 name1 | Director | 2 | Human Resource | 2 |
000012 | emp2 name2 | employee | 2 | Human Resource | 2 |
000013 | emp2 name2 | Director | 3 | Recruitment | 3 |
000200 | emp2 name2 | Head Director | 1 | CEO | 1 |
我该怎么办?
P.S。对不起我的英语
WITH cte AS ( SELECT *, MAX(d.dept_level) OVER (PARTITION BY e.emp_id) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3) )
SELECT *
FROM cte
WHERE dept_level = maxlevel
ORDER BY emp_id
对于 MySQL 5.7 / MariaDB 10.1 使用
SELECT *
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
JOIN ( SELECT e.emp_id,
MAX(d.dept_level) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
GROUP BY e.emp_id ) ed USING (emp_id)
WHERE d.dept_level = ed.maxlevel
ORDER BY emp_id