下面给出的是我在sql中与count相关的查询,我想要预期的输出

时间:2019-04-10 07:38:28

标签: mysql mysqli

我编写了此查询,该查询向我提供了playlist_details表中特定歌曲的总数。 (示例:让我们假设歌曲名称为“ XYZ”,并且在playlist_details表中存在三个具有三个不同日期的条目,因此我的查询将输出显示为“ count 3”。现在,我进一步希望对该查询进行分叉,以便我在此count列旁边需要再增加一列,它显示了歌曲xyz在表中重复的三个日期,因此我将count设为3。

SELECT
playlist_details.`EPISODE_TITLE` AS SongName,
songs_master.`ASSET_ID`,
song_rights_master.`CHANNEL_IDS` AS VChannels,
channel_master.`PLAYLIST_EXPORT_FILE_NAME`,
playlist_details.`DURATION`,
Count(*) AS Count,
programmes_master.`TITLE` as moviename,
songs_master.`SONG_TYPE`
FROM
playlist_details
INNER JOIN songs_master ON playlist_details.`ASSET_ID` = songs_master.`ASSET_ID`
INNER JOIN song_rights_master ON song_rights_master.`SONG_ID` = songs_master.`SONG_ID`
INNER JOIN channel_master ON channel_master.`CHANNEL_ID` = song_rights_master.`CHANNEL_IDS`
LEFT JOIN programmes_master ON songs_master.`PROGRAMME_ID` = programmes_master.`PROGRAMME_ID`
WHERE playlist_details.`EVENT_TYPE` = 'Song' AND
song_rights_master.`songs_agreements_master_id`='".$agreement."'
GROUP BY SongName

当前输出:

+------------------------+
| SongName AssetID Total |
+------------------------+
| XYZ      000000  3     |
+------------------------+

预期输出:

+-------------------------------------+
| SongName AssetID ScheduleDate Total |
+-------------------------------------+
| XYZ      000000  13-09-2019   3     |
|                  14-09-2019         |
|                  15-09-2019         |
+-------------------------------------+

1 个答案:

答案 0 :(得分:0)

我真的不知道您期望的输出格式,您似乎想要仅包含日期的其他结果行?如果您有多个结果,将它们链接到您的歌曲将非常困难。.我建议一种解决方案,针对每个结果将日期以唯一行的形式输出为字符串,并以逗号分隔,如下所示:

+-----------------------------------------------------------+
| SongName AssetID ScheduleDate                       Total |
+-----------------------------------------------------------+
| XYZ      000000  13-09-2019,14-09-2019,15-09-2019   3     |
+-----------------------------------------------------------+

为此,您可以使用GROUP_CONCAT来连接与GROUP BY分组的值(假设该字段在数据库中被命名为ScheduleDate

SELECT
playlist_details.`EPISODE_TITLE` AS SongName,
songs_master.`ASSET_ID`,
song_rights_master.`CHANNEL_IDS` AS VChannels,
channel_master.`PLAYLIST_EXPORT_FILE_NAME`,
playlist_details.`DURATION`,
Count(*) AS Count,
GROUP_CONCAT(playlist_details.`ScheduleDate`) as ScheduleDate,
programmes_master.`TITLE` as moviename,
songs_master.`SONG_TYPE`
FROM
playlist_details
INNER JOIN songs_master ON playlist_details.`ASSET_ID` = songs_master.`ASSET_ID`
INNER JOIN song_rights_master ON song_rights_master.`SONG_ID` = songs_master.`SONG_ID`
INNER JOIN channel_master ON channel_master.`CHANNEL_ID` = song_rights_master.`CHANNEL_IDS`
LEFT JOIN programmes_master ON songs_master.`PROGRAMME_ID` = programmes_master.`PROGRAMME_ID`
WHERE playlist_details.`EVENT_TYPE` = 'Song' AND
song_rights_master.`songs_agreements_master_id`='".$agreement."'
GROUP BY SongName

See a simple example here