在MySQL中的一个表上使用两个连接?

时间:2014-03-02 00:50:50

标签: mysql sql join

由于添加了查询的中间部分(INNER JOIN),查询现在失败了。我想这可能是因为我在一个表上有两种类型的连接(charts_extended) - 我看不到另一种从dj_love表中获取所需数据的方法。

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 

1 个答案:

答案 0 :(得分: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