我正在讨论从3个表中提取唯一值的查询。这是在2个单独的查询中做得更好吗?
查询是:
count as returned (
all leadID from lds where status = "ok"
AND leadID is also in rlds with recID="999"
AND rdate > (03-20-2012)
+
(all distinct leadID from plds where recID="999"
AND change != NULL
AND pdate > (03-20-2012))
工作查询的结果应为“2”:leadID 1和leadID 4
表lds:
leadID |状态
1 | OK
2 |坏
3 | OK
表rlds:
leadID | recID2 | RDATE
1 | 999 | 2012年3月27日
6 | 999 | 2012年3月27日
谢谢!
答案 0 :(得分:1)
SELECT Ids.leadId
FROM
Ids JOIN
rlds ON rlds.leadId = Ids.LeadId AND recID = 999 AND rdate > '03-20-2012'
WHERE Ids.Status = 'ok'
UNION
SELECT leadId
FROM pIds
WHERE recID = 999 AND change IS NOT NULL AND pdate > '03-20-2012'
答案 1 :(得分:0)
您可以使用UNION关键字将数组检索为2行,第一行包含您的第一场比赛的计数,并将第二场比赛的计数进行借调。
然后,您可以使用SUM关键字在封闭选择中将两行相加。结果将是一行争论您的计数。
SELECT sum(1)
FROM
(
SELECT count(1)
FROM lds
INNER JOIN rlds ON lds.leadid = plds.leadid
WHERE lds.status='ok' AND rlds.recid2=999 AND rlds.pdate > '03-20-2012'
UNION
SELECT count(1)
FROM plds
WHERE plds.recid=999
AND change != NULL
AND pdate > '03-20-2012'
) AS tmpcount