多个表 select,子查询返回了超过 1 个值。 SQL
multiple tables select, Subquery returned more than 1 value. SQL
我有两个表:
员工
id | fullName | birth | speciality
1 | A A A | 01/01/1980 | Manager
2 | B B B | 01/01/1980 | Developer
3 | C C C | 01/01/1980 | User
员工状况
ID | status | dateChange
1 | 1 | 01/01/2010
2 | 1 | 01/01/2013
3 | 1 | 01/01/2015
3 | 2 | 01/01/2016
我想选择以下数据
ID | Full name | Bith date | speciality | Date hired | Date fired
结果必须是:
ID | Full name | Bith date | speciality | Date hired | Date fired
1 | A A A | 01/01/1980 | Manager | 01/01/2010 | null
2 | B B B | 01/01/1980 | Developer | 01/01/2013 | null
3 | C C C | 01/01/1980 | User | 01/01/2015 | 01/01/2016
3 | C C C |01/01/1980 | User | 01/01/2017 | null
我的代码:
SELECT Employees.id , Employees.fullName, Employees.birth, Employees.speciality,
(SELECT dateChange FROM EmployeesStatus WHERE status=1 AND id=Employees.id) datehired,
(SELECT dateChange FROM EmployeesStatus WHERE status=2 AND id=Employees.id) datefired FROM Employees
导致以下消息:
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= ,
, >= or when the subquery is used as an expression.
有什么想法吗?
尝试以下操作:
SELECT E.ID,E.FULLNAME,E.BIRTH,E.SPECIALITY,ED.DATE_HIRED,ED.DATE_FIRED
FROM EMPLOYEES E,
(SELECT ID,
MAX(CASE WHEN STATUS=1 THEN DATECHANGE ELSE NULL END)DATE_HIRED,
MAX(CASE WHEN STATUS=2 THEN DATECHANGE ELSE NULL END)DATE_FIRED
FROM EMPLOYEESSTATUS
GROUP BY ID)ED
WHERE E.ID=ED.ID
此查询在性能方面会更快,并且会得到相同的结果。
您应该使用基于子查询的联接而不是 =
SELECT
Employees.id
, Employees.fullName
, Employees.birth
, Employees.speciality
, e1.dateChange as datehired, e2.dateChange as datefired
FROM Employees
INNER JOIN EmployeesStatus es1 on e1.status=1 AND e1.id=Employees.id
LEFT JOIN EmployeesStatus es2 on e2.status=2 AND e2.id=Employees.id
或者您可以在子查询中使用 in 子句而不是 =
除了@scaisEdge 回答:
SELECT
Employees.id,
Employees.fullName,
Employees.birth,
Employees.speciality
e1.dateChange as datehired,
MIN(e2.dateChange) as datefired
FROM Employees
INNER JOIN EmployeesStatus es1 on e1.status=1 AND e1.id=Employees.id
LEFT JOIN EmployeesStatus es2 on e2.status=2 AND e2.id=Employees.id
AND e2.dateChange > e1.dateChange
GROUP BY Employees.id,
Employees.fullName,
Employees.birth,
Employees.speciality,
e1.dateChange
试试这个
SELECT e.id ,
e.fullName,
e.birth,
e.speciality,
CASE WHEN t1.status = 1 then max(t1.dateChange) else null end as "Date Hired",
CASE WHEN t2.status = 2 then max(t2.dateChange) else null end as "Date Fired",
FROM Employees e
LEFT JOIN EmployeesStatus t1 on t1.id = e.id and t1.status = 1
LEFT JOIN EmployeesStatus t2 on t2.id = e.id and t1.status = 2
希望这有效..
我有两个表:
员工
id | fullName | birth | speciality
1 | A A A | 01/01/1980 | Manager
2 | B B B | 01/01/1980 | Developer
3 | C C C | 01/01/1980 | User
员工状况
ID | status | dateChange
1 | 1 | 01/01/2010
2 | 1 | 01/01/2013
3 | 1 | 01/01/2015
3 | 2 | 01/01/2016
我想选择以下数据
ID | Full name | Bith date | speciality | Date hired | Date fired
结果必须是:
ID | Full name | Bith date | speciality | Date hired | Date fired
1 | A A A | 01/01/1980 | Manager | 01/01/2010 | null
2 | B B B | 01/01/1980 | Developer | 01/01/2013 | null
3 | C C C | 01/01/1980 | User | 01/01/2015 | 01/01/2016
3 | C C C |01/01/1980 | User | 01/01/2017 | null
我的代码:
SELECT Employees.id , Employees.fullName, Employees.birth, Employees.speciality,
(SELECT dateChange FROM EmployeesStatus WHERE status=1 AND id=Employees.id) datehired,
(SELECT dateChange FROM EmployeesStatus WHERE status=2 AND id=Employees.id) datefired FROM Employees
导致以下消息:
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= ,
, >= or when the subquery is used as an expression.
有什么想法吗?
尝试以下操作:
SELECT E.ID,E.FULLNAME,E.BIRTH,E.SPECIALITY,ED.DATE_HIRED,ED.DATE_FIRED
FROM EMPLOYEES E,
(SELECT ID,
MAX(CASE WHEN STATUS=1 THEN DATECHANGE ELSE NULL END)DATE_HIRED,
MAX(CASE WHEN STATUS=2 THEN DATECHANGE ELSE NULL END)DATE_FIRED
FROM EMPLOYEESSTATUS
GROUP BY ID)ED
WHERE E.ID=ED.ID
此查询在性能方面会更快,并且会得到相同的结果。
您应该使用基于子查询的联接而不是 =
SELECT
Employees.id
, Employees.fullName
, Employees.birth
, Employees.speciality
, e1.dateChange as datehired, e2.dateChange as datefired
FROM Employees
INNER JOIN EmployeesStatus es1 on e1.status=1 AND e1.id=Employees.id
LEFT JOIN EmployeesStatus es2 on e2.status=2 AND e2.id=Employees.id
或者您可以在子查询中使用 in 子句而不是 =
除了@scaisEdge 回答:
SELECT
Employees.id,
Employees.fullName,
Employees.birth,
Employees.speciality
e1.dateChange as datehired,
MIN(e2.dateChange) as datefired
FROM Employees
INNER JOIN EmployeesStatus es1 on e1.status=1 AND e1.id=Employees.id
LEFT JOIN EmployeesStatus es2 on e2.status=2 AND e2.id=Employees.id
AND e2.dateChange > e1.dateChange
GROUP BY Employees.id,
Employees.fullName,
Employees.birth,
Employees.speciality,
e1.dateChange
试试这个
SELECT e.id ,
e.fullName,
e.birth,
e.speciality,
CASE WHEN t1.status = 1 then max(t1.dateChange) else null end as "Date Hired",
CASE WHEN t2.status = 2 then max(t2.dateChange) else null end as "Date Fired",
FROM Employees e
LEFT JOIN EmployeesStatus t1 on t1.id = e.id and t1.status = 1
LEFT JOIN EmployeesStatus t2 on t2.id = e.id and t1.status = 2
希望这有效..