2014-03-02 57 views
0

由於添加查詢的中間部分(INNER JOIN),查詢現在失敗。我認爲這可能是因爲我在一個表上有兩種類型的連接(charts_extended) - 我看不到從dj_love表獲取所需數據的另一種方式。在MySQL的一個表上使用兩個連接?

SELECT DISTINCT releases . * 
FROM releases 
INNER JOIN artist_love ON releases.all_artists LIKE CONCAT('%', artist_love.artist, '%') 
AND artist_love.user = 'Quickinho' 
INNER JOIN label_love ON releases.label_no_country = label_love.label 
AND label_love.user = 'Quickinho' 

INNER JOIN charts_extended ON charts_extended.release_id = releases.id 
WHERE charts_extended.artist 
IN (

    SELECT dj 
    FROM dj_love 
    WHERE user = 'Quickinho' 
    ) 

LEFT JOIN charts_extended ON charts_extended.release_id = releases.id 
AND charts_extended.artist = 'Quickinho' 
WHERE charts_extended.release_id IS NULL 

ORDER BY releases.date DESC 
+0

要不要再來JOIN - 無需 – Strawberry

+0

在那裏你推薦添加JOIN子查詢? – Franco

回答

1

試試這個:

SELECT DISTINCT releases . * 
FROM releases 
INNER JOIN artist_love 
    ON releases.all_artists LIKE CONCAT('%', artist_love.artist, '%') AND artist_love.user = 'Quickinho' 
INNER JOIN label_love 
    ON releases.label_no_country = label_love.label AND label_love.user = 'Quickinho' 
INNER JOIN charts_extended as a 
    ON a.release_id = releases.id 
LEFT JOIN charts_extended as b 
    ON b.release_id = releases.id 
     AND b.artist IN (SELECT dj FROM dj_love WHERE user = 'Quickinho') 
     AND b.artist = 'Quickinho' 
WHERE 
    b.release_id IS NULL 

ORDER BY releases.date DESC 
+0

這是行得通的(一旦我給INNER JOIN charts_extended分配了一個別名),但它似乎忽略了LEFT JOIN的'WHERE charts_extended.release_id IS NULL'部分。有任何想法嗎?謝謝你的幫助! – Franco

+0

WHERE不是左連接的一部分,它是過濾的一部分,如果你選擇這個列,你會看到只有NULL值,我注意到了混淆的問題 - 檢查更新後的查詢 –