我正在使用MySQL。這裏是我的架構:MySQL:無法使用組功能
供應商(SID:整數,SNAME:字符串,地址字符串)
零件(PID:整數,PNAME:字符串,顏色:字符串)
目錄( SID:整數,PID:整數,成本:實數)
(主鍵以粗體顯示)
我噸rying編寫一個查詢來選擇由至少兩家供應商的所有部分:
-- Find the pids of parts supplied by at least two different suppliers.
SELECT c1.pid -- select the pid
FROM Catalog AS c1 -- from the Catalog table
WHERE c1.pid IN ( -- where that pid is in the set:
SELECT c2.pid -- of pids
FROM Catalog AS c2 -- from catalog
WHERE c2.pid = c1.pid AND COUNT(c2.sid) >= 2 -- where there are at least two corresponding sids
);
首先,我我甚至要對這個正確的方式?
其次,我得到這個錯誤:
1111 - Invalid use of group function
我在做什麼錯?
所以'HAVING'先計算過濾器,是嗎? – 2013-07-17 07:29:11
另外,如果使用GROUP BY,HAVING應該在GROUP BY – Viacheslav 2014-06-22 17:40:34
之後。此外,GROUP BY需要在HAVING之前....應該閱讀Bandolero的評論:D – Andrew 2014-12-16 16:52:58