将子查询转换为Join

时间:2014-07-26 00:01:48

标签: mysql join

我想将此查询转换为连接系列,因为它当前正在杀死db

SELECT releases.* FROM releases WHERE label_no_country IN (SELECT releases.label_no_country
                    FROM releases
                    INNER JOIN charts_extended
                    ON charts_extended.release_id=releases.id AND charts_extended.artist='Quickinho'
                    GROUP BY releases.label_no_country
                    ORDER BY count(releases.label_no_country) DESC
                    )
                    AND releases.id NOT IN (SELECT release_id FROM charts_extended WHERE artist='Quickinho')
                    AND releases.all_artists!='Various Artists'
                    AND releases.label_no_country!='Unknown Label'
                    ORDER BY date DESC
                    LIMIT 0,12

1 个答案:

答案 0 :(得分:2)

SELECT r.* 
  FROM releases r
  JOIN charts_extended x
    ON x.release_id = r.id 
   AND x.artist = 'Quickinho'
   AND x.label_no_country = r.label_no_country
  LEFT
  JOIN charts_extended y 
    ON y.release_id = r.id
 WHERE r.all_artists! = 'Various Artists'
   AND r.label_no_country != 'Unknown Label'
   AND y.release_id IS NULL
 ORDER 
    BY date DESC
 LIMIT 0,12

或类似的东西。除此之外,我们需要看到一个EXPLAIN和正确的DDL