数据库
首页 > 数据库> > 如何在MySQL子查询中按MAX(日期)排序?

如何在MySQL子查询中按MAX(日期)排序?

作者:互联网

我对此查询感到麻烦:

我有两个表:样本和歌曲
我想选择

pathName   from samples
id_user    from samples
id         from songs
name       from songs

每个样本都属于一首歌曲:我想将共享相同歌曲ID的每个样本归为一组,以便选择最后的日期.
所以我像这样在两个表之间进行联接:

$query = 'SELECT 
            samples.pathName path_name,
            samples.id_user id_user,
            songs.id id_song,
            songs.name song_name
        FROM (SELECT *, MAX(date) AS maxDate FROM samples GROUP BY id_song ORDER BY maxDate) samples
        INNER JOIN songs songs
        ON songs.id = samples.id_chanson
        WHERE songs.finished = false';

它不应该那么重,我希望我很清楚…

标签:subquery,mysql
来源: https://codeday.me/bug/20191123/2065679.html