SELECT requestID
FROM request
WHERE userId = (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept = (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
问问题
29841 次
2 回答
13
使用以下方式编写此查询会更清晰JOIN
:
select distinct r.requestID
from
request r
join department d
on d.userId = r.userID
and desig = 'E'
join department d2
on d2.dept = d.dept
and d2.desig = 'FM'
and d2.userId = 'it18'
或者,您可以简单地替换=
为IN
:
SELECT requestID
FROM request
WHERE userId IN (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept IN (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
它们应该返回相同的结果,但同时尝试两者以查看性能是否有任何差异。
于 2013-03-15T19:34:47.030 回答
6
有两种处理方法:
选项 1:更改为WHERE ... IN (SELECT ...)
,如下所示:
SELECT requestID
FROM request
WHERE userId IN (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept IN (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
选项 2:使用聚合函数(如 )仅强制一个结果MAX()
,如下所示:
SELECT requestID
FROM request
WHERE userId = (
SELECT MAX(userID)
FROM department
WHERE desig = 'E'
AND dept = (
SELECT MAX(dept)
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
于 2013-03-15T19:39:58.570 回答