我有2张桌子-部门和员工。
员工表:department_id是来自部门表(id列)的外键
|-------------|------------------|---------------|-----------------|
| id | employee_no | department_id |employee_manager |
|-------------|------------------|---------------|-----------------|
| 1 | 34 | 1 | Robert |
| 2 | 34 | 1 | Timothy |
| 3 | 35 | 1 | John |
| 4 | 36 | 2 | Benjamin |
| 5 | 36 | 2 | Bryan |
|-------------|------------------|---------------|-----------------|
部门表:
|-------------|------------------|---------------|
| id | department_name | dept_location |
|-------------|------------------|---------------|
| 1 | Billing | CA |
| 2 | Marketing | NV |
|-------------|------------------|---------------|
我需要一个有关SQL查询的帮助,该查询返回雇员表中符合以下条件的,与部门表中的department_id匹配的所有行。
如果部门ID匹配多个非唯一的employee_no(例如,department_id 1匹配employee_no 34和35),则联接应从部门表中获得dept_location为“ CA”。
如果部门ID与唯一的employee_no匹配不止一次(例如,Department_id 2两次与employee_no 36匹配两次),则部门表中的联接不适用,结果应为“ NA”(不适用) )用于dept_location
我的结果应类似于下表:
|-------------|------------------|---------------------|-----------------|
| id | employee_no | department_location |employee_manager |
|-------------|------------------|---------------------|-----------------|
| 1 | 34 | CA | Robert |
| 2 | 34 | CA | Timothy |
| 3 | 35 | CA | John |
| 4 | 36 | NA | Benjamin |
| 5 | 36 | NA | Bryan |
|-------------|------------------|---------------------|-----------------|
答案 0 :(得分:1)
不确定我是否理解,但是在我看来,您可以使用派生字段,或者更糟的是,可以使用IF在两个不同的派生公式之间进行选择:
SELECT id, employee_no,
CASE (SELECT COUNT(*) FROM employees AS e WHERE e.department_id = employees.department_id)
WHEN 2 THEN 'CA' ELSE 'NA'
END AS department_location,
employee_manager FROM employees;
CREATE TABLE department ( id integer, department_name varchar(30), dept_location varchar(30));
INSERT INTO department VALUES (1, 'Billing', 'CA'), (2, 'Marketing', 'NV');
CREATE TABLE employees (id integer, employee_no integer, department_id integer, employee_manager varchar(30));
INSERT INTO employees VALUES
(1, 34, 1, 'Robert'),
(2, 34, 1, 'Timothy'),
(3, 35, 1, 'John'),
(4, 36, 2, 'Benjamin'),
(5, 36, 2, 'Bryan');
然后SELECT 出现起作用:
SELECT id, employee_no, CASE (SELECT COUNT(*) FROM employees AS e WHERE e.department_id = employees.department_id) WHEN 2 THEN 'NA' ELSE 'CA' END AS department_location, employee_manager FROM employees;
+------+-------------+---------------------+------------------+
| id | employee_no | department_location | employee_manager |
+------+-------------+---------------------+------------------+
| 1 | 34 | CA | Robert |
| 2 | 34 | CA | Timothy |
| 3 | 35 | CA | John |
| 4 | 36 | NA | Benjamin |
| 5 | 36 | NA | Bryan |
+------+-------------+---------------------+------------------+
5 rows in set (0.00 sec)
答案 1 :(得分:1)
这是一个将在8.0之前的MySQL版本中运行的查询。它使用派生的每个部门不同员工计数表来确定是显示部门位置还是显示NA
:
SELECT e.id, e.employee_no,
CASE WHEN c.distinct > 1 THEN d.dept_location
ELSE 'NA'
END AS department_location,
e.employee_manager
FROM employees e
JOIN (SELECT department_id, COUNT(DISTINCT employee_no) AS `distinct`
FROM employees
GROUP BY department_id) c ON c.department_id = e.department_id
JOIN department d ON d.id = e.department_id
输出:
id employee_no employee_manager department_location
1 34 Robert CA
2 34 Timothy CA
3 35 John CA
4 36 Benjamin NA
5 36 Bryan NA
答案 2 :(得分:0)
这是一个依赖于窗口函数(MySQL 8.0中提供)的解决方案,从而避免了子查询的需要。
诀窍是比较每个部门的最小和最大员工人数。如果他们不同,那么我们可以肯定地知道给定部门属于一个以上的不同员工。
SELECT
e.id,
e.employee_no,
CASE
WHEN MAX(e.employee_no) OVER(PARTITION BY d.id)
= MIN(e.employee_no) OVER(PARTITION BY d.id)
THEN 'NA'
ELSE 'CA'
END department_location,
e.employee_manager
FROM employee e
INNER JOIN department d ON e.department_id = d.id
ORDER BY e.id
此 demo on DB Fiddle 及其示例数据将返回:
| id | employee_no | employee_manager | department_location |
| --- | ----------- | ---------------- | ------------------- |
| 1 | 34 | Robert | CA |
| 2 | 34 | Timothy | CA |
| 3 | 35 | John | CA |
| 4 | 36 | Benjamin | NA |
| 5 | 36 | Bryan | NA |