有幾種方法可以返回指定的結果。
假設規範是從batch
在batchdetails
具有至少兩個相關聯的行返回僅行,並且這些相關聯的行中的至少一個是subtechid=1
和至少一個與subtechid=2
對於MySQL:
SELECT b.batchname
FROM batch b
JOIN batchdetail d1
ON d1.batchid = b.batchid
AND d1.subtechid = 1
JOIN batchdetail d2
ON d2.batchid = b.batchid
AND d2.subtechid = 2
WHERE b.techid = 1
GROUP BY b.batchid
ORDER BY b.batchname
也有可能獲得使用EXISTS
謂詞同樣的結果:
SELECT b.batchname
FROM batch b
WHERE b.techid = 1
AND EXISTS (SELECT 1
FROM batchdetail d1
WHERE d1.batchid = b.batchid
AND d1.subtechid = 1
)
AND EXISTS (SELECT 1
FROM batchdetail d2
WHERE d2.batchid = b.batchid
AND d2.subtechid = 2
)
ORDER BY b.batchname
,或者讓subtechid和比較不同值的數量爲恆定
SELECT b.batchname
FROM batch b
JOIN batchdetail d
ON d.batchid = b.batchid
AND d.subtechid IN (1,2)
WHERE b.techid = 1
GROUP BY b.batchid
HAVING COUNT(DISTINCT d.subtechid) = 2
ORDER BY b.batchname
還有一些其他的查詢模式...
爲什麼這個標籤甲骨文*和* MySQL的? – tadman
賈斯汀,你想要查詢每種查詢語言,或者什麼?請註明 – Fallenreaper