需要 PHP 和 MySQL 方面的帮助ߪ

发布于 2024-11-15 14:17:47 字数 965 浏览 2 评论 0原文

我有一个歌曲表,有些歌曲是专辑歌曲,有些是单曲...我有一个专辑表...

歌曲表列:Song_ID、Album_ID、Song_Name、Date_Released、Timestamp_Released , 其他的... 如果Album_ID为[null],则表示该歌曲是单个

专辑表,列:Album_ID、Album_Name、Song_IDs、Date_Released、其他...

注1:有相册表中没有 Timestamp_Released

注 2:Date_Released 只是没有时间的一天,即“2011-06-16”

我目前正在使用此查询来显示一个表格(在我的 html/php 页面中),其中每一行都是一首单曲或一张专辑(专辑中的歌曲全部显示在一行中作为专辑) )

SELECT 
    IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name, 
    IF(Album_ID IS NULL,s.Date_Released,a.Date_Released) as datereleased, 
    s.Timestamp_Released
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1,2
ORDER BY 2 DESC,1
LIMIT 0,10; 

上面的查询根据日期对歌曲和专辑列表进行排序,并为专辑提供专辑中最古老歌曲的 Date_ReleasedTimestamp_Released...

所以我的问题如何为专辑提供最新歌曲的 Date_ReleasedTimestamp_Released

谢谢 :)

I have a table of songs, some songs are album song, and some are singles... And I have a table of albums...

Songs table cols: Song_ID, Album_ID, Song_Name, Date_Released, Timestamp_Released, others...
If the Album_ID is [null], it means the song is a single

Albums table cols: Album_ID, Album_Name, Song_IDs, Date_Released, others...

Note 1: there is no Timestamp_Released in albums table

Note 2: Date_Released is only a day with no time i.e. "2011-06-16"

I'm currently using this query to display a table (in my html/php page) that each row is a single or a album (songs that are in an album are displayed all in one row as album)

SELECT 
    IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name, 
    IF(Album_ID IS NULL,s.Date_Released,a.Date_Released) as datereleased, 
    s.Timestamp_Released
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1,2
ORDER BY 2 DESC,1
LIMIT 0,10; 

The query above order the list of songs and albums according to date and give the albums the Date_Released and Timestamp_Released of the oldest song in the album...

So my question is how to give the album the Date_Released and Timestamp_Released of the newest song in it ?

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-11-22 14:17:47

s.Date_Released, s.Timestamp_Released 写入 MAX(s.Date_Released) 作为 Newest_Date_Released,MAX(s.Timestamp_Released) 作为 Newest_Timestamp_Releasd
更新

SELECT 
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name, 
MAX(IF(Album_ID IS NULL,s.Date_Released,a.Date_Released)) as datereleased, 
MAX(s.Timestamp_Released)
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 0,10; 

Instead of s.Date_Released, s.Timestamp_Released write MAX(s.Date_Released) as Newest_Date_Released, MAX(s.Timestamp_Released) as Newest_Timestamp_Releasd
UPDATE

SELECT 
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name, 
MAX(IF(Album_ID IS NULL,s.Date_Released,a.Date_Released)) as datereleased, 
MAX(s.Timestamp_Released)
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 0,10; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文