我需要显示一个没有指定主管在同一部门工作的员工名单
Table: employee
Fields:
id INT(pk),
department_id INT,
chief_id INT,
name Varchar(100),
salary INT
查询:
SELECT One.name AS Employee,One.department_id, Two.name AS Chief, Two.department_id
FROM employee One, employee Two
WHERE (One.chief_id = Two.id AND One.department_id != Two.department_id) OR One.department_id IS NULL;
答案 0 :(得分:1)
SELECT One.name AS Employee,One.department_id, Two.name AS Chief, Two.department_id
FROM employee One
LEFT JOIN employee two ON One.chief_id = Two.id
WHERE NOT EXISTS (
SELECT 0 FROM employee two
where One.department_id = Two.department_id
AND One.chief_id = Two.id
)
答案 1 :(得分:0)
尝试以下内容;)
SELECT One.name AS Employee,One.department_id, Two.name AS Chief, Two.department_id
FROM employee One, employee Two
WHERE One.chief_id = Two.id AND (One.department_id IS NULL OR One.department_id <> Two.department_id);
答案 2 :(得分:0)
以下查询可能会提供所需的结果: -
SQL SERVER : -
1, product1, content1, 10
2, product2, content2, 30
3, product1, content3, 20
4, product3, content4, 0
5, product2, content5, 35
6, product3, content6, 0
MYSQL : -
SELECT One.name AS Employee,One.department_id, Two.name AS Chief, Two.department_id
FROM employee One, employee Two
WHERE (One.chief_id = Two.id AND One.department_id != Two.department_id) OR ISNULL(One.department_id,0)=0;
答案 3 :(得分:0)
尝试查询
SELECT
Two.name AS Employee,
Two.department_id AS Employee_Dept,
One.name AS Chief_Name,
One.department_id AS Chif_Dept
FROM
employee ONE
INNER JOIN employee Two
ON One.id = Two.chief_id
AND One.department_id != Two.department_id ;