2013-11-22 50 views
0

我發現,MySql不支持連接中的相關查詢。是否有可能重寫提供(簡單)查詢保持邏輯?MySQL如何在連接中使用相關查詢

SELECT * 
FROM tableX AS x 
LEFT JOIN ( 
    SELECT y.xFK, COUNT(y.id) AS y_count 
    FROM tableY AS y 
    WHERE y.xFK = x.id 
    GROUP BY y.xFK 
) AS sub 
ON sub.xFK = x.id 
WHERE sub.y_count > 1 

在此先感謝。

回答

0

這是你想要的嗎?

SELECT 
    x.*, COUNT(y.ID) AS y_count 
FROM tableX x 
    LEFT JOIN tableY y 
    ON y.xFK = x.ID 
GROUP BY 
    x.ID 
HAVING 
    y_count > 1