需要帮助以某种方式配置查询输出

时间:2010-10-10 15:45:05

标签: sql mysql database database-design

我们有一个包含电视频道列表的表格。

create table listing (id, channel_name, event_name, event_time)

a) listing (1, "ABC Channel", "All My Children", 6.00 AM)
b) listing (2, "ABC Channel", "News", 7 AM)
c) listing (3, "ABC Channel", "One life to live", 7.15 AM)
d) listing (4, "ABC Channel", "General Hospital", 8.00 AM)
e) listing (5, "ABC Channel", "News", 9.00 AM)
f) listing (6, "ABC Channel", "Movie 1", 9.15 AM)
g) listing (7, "ABC Channel", "News", 10.00 AM)

我的网页显示了一个特定的电视频道列表,如下所示: -

event_name        event_times
---------------------------------------------
All My Children   6.00 AM
News              7.00 AM, 9.00 AM, 10.00 AM
One life to live  7.15 AM
General Hospital  8.00 AM
Movie 1           9.15 AM

所有新闻时间都合并为一个条目,目前我正在整合应用层的条目,是否可以在查询级别执行此操作。

2 个答案:

答案 0 :(得分:3)

尝试这样的事情:

  SELECT event_name,
         GROUP_CONCAT(event_time SEPARATOR ', ' ORDER BY event_time) AS event_times
    FROM listing
GROUP BY event_name
ORDER BY event_name

如果您需要单独的频道,这应该有效:

  SELECT event_name,
         GROUP_CONCAT(event_time SEPARATOR ', ' ORDER BY event_time) AS event_times
    FROM listing
   WHERE channel_name = 'ABC Channel'
GROUP BY event_name
ORDER BY event_name

答案 1 :(得分:1)

也许UNION非新闻以及新闻行。只有新闻需要连接它的时代。

SELECT event_name,
       event_times
FROM  (
  SELECT
    event_name, event_time
  FROM listing
  WHERE event_name != 'News' 
  UNION
  SELECT event_name, GROUP_CONCAT(event_time SEPARATOR ', ') event_time
  FROM listing
  WHERE event_name = 'News' 
  ) AS All 
ORDER BY event_times ASC