diff --git a/storage/mariadb/track.go b/storage/mariadb/track.go index e16f1834..1ba7dd43 100644 --- a/storage/mariadb/track.go +++ b/storage/mariadb/track.go @@ -485,29 +485,35 @@ func (ss SongStorage) UpdateHashLink(old, new radio.SongHash) error { } var songFavoritesOfQuery = expand(` +WITH + esong +AS (SELECT DISTINCT + esong.* + FROM + enick + JOIN + efave ON efave.inick = enick.id + JOIN + esong ON esong.id = efave.isong + WHERE + enick.nick = ?) SELECT {songColumns}, {maybeTrackColumns}, - {lastplayedSelect}, + COALESCE(eplay.dt, TIMESTAMP('0000-00-00 00:00:00')) AS lastplayed, NOW() AS synctime FROM esong LEFT JOIN tracks ON tracks.hash = esong.hash -JOIN - (SELECT DISTINCT - esong.hash_link, - efave.id +LEFT JOIN + (SELECT + MAX(dt) AS dt, + isong FROM - enick - JOIN - efave ON efave.inick = enick.id - JOIN - esong ON esong.id = efave.isong - WHERE - enick.nick = ?) AS truth - ON esong.hash = truth.hash_link -ORDER BY truth.id ASC + eplay + GROUP BY + isong) AS eplay ON eplay.isong = esong.id LIMIT ? OFFSET ?; `) @@ -515,15 +521,15 @@ var songFavoritesOfDatabaseOnlyQuery = expand(` WITH esong AS (SELECT DISTINCT - esong.* -FROM - enick -JOIN - efave ON efave.inick = enick.id -JOIN - esong ON esong.id = efave.isong -WHERE - enick.nick = ?) + esong.* + FROM + enick + JOIN + efave ON efave.inick = enick.id + JOIN + esong ON esong.id = efave.isong + WHERE + enick.nick = ?) SELECT {songColumns}, {trackColumns}, @@ -533,7 +539,7 @@ FROM tracks JOIN esong ON tracks.hash = esong.hash -JOIN +LEFT JOIN (SELECT MAX(dt) AS dt, isong