优化多列的mysql子查询

时间:2012-01-24 19:02:40

标签: mysql

我有一个展示表,里面有25万条记录。 show table中的列是id,showid,artist_id,cityid,created。

如果同样的节目发生了4天。然后在系统数据中会像:

为每个节目创建的

id,showid,artist_id,cityid,showid将保持不变。

1 245 1 3 2011-12-30 15:00:00

2 245 1 3 2011-12-30 13:00:00

3 245 1 3 2011-12-30 19:00:00

SELECT UT.user_id,UT.type_id, UT.type ,UT.track_time, 
(SELECT showid FROM shows WHERE FIND_IN_SET(UT.type_id,artist_id) ORDER BY created DESC LIMIT 1)   as mainshowid,
(SELECT cityid FROM shows WHERE FIND_IN_SET(UT.type_id,artist_id) ORDER BY created DESC LIMIT 1)   as maincityid
FROM user_details as UT  WHERE UT.type='artist' ORDER BY UT.track_time desc limit 20;

查询给出了欲望输出但我如何在单个SUB查询中或通过任何其他方式获得多个列。

1 个答案:

答案 0 :(得分:0)

你可以:

SELECT 
    UT.user_id,
    UT.type_id, 
    UT.type,
    UT.track_time, 
    s.showid AS mainshowid,
    s.cityid AS maincityid
FROM 
      user_details AS UT 
  JOIN
      shows AS s
    ON s.showid =
       ( SELECT showid 
         FROM shows 
         WHERE FIND_IN_SET(UT.type_id, artist_id)
         ORDER BY created DESC 
         LIMIT 1
       ) 
WHERE UT.type='artist' 
ORDER BY UT.track_time DESC 
LIMIT 20 ;