我有一个包含以下列的表:
playlistid
trackname
playstatus
likes
dislikes
created
播放状态可以有以下值:
0, 1, 2 and 4
。我现在使用的sql查询是:
SELECT * from playlist
WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC
值得一提的是,在任何时候只有一行playstatus=1,一行playlistid=3,但是会有多行playstatus=0和2。
我想更改上面的查询,以便在一个查询中从where playstatus=3获取1行,从where playstatus=1获取1行,从where playstatus=0获取3行。所有这些都必须基于您在上面的查询中看到的相同orderby来选择。
感谢大家的帮助!谢谢你抽出时间!
最佳答案
尝试此查询
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC limit 3
关于php - MySQL多个子查询限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9785964/