Mysql多子查询限制

时间:2012-03-20 11:33:53

标签: php mysql

我有一个包含以下列的表:

playlistid 
trackname 
playstatus
likes
dislikes
created

playstatus可以具有以下值: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.

我想更改上面的查询,以便我从playstatus = 3获取1行,playstatus = 1中的1行,以及单个查询中playstatus = 0的3行。所有这些都必须根据您在上面的查询中看到的相同顺序进行选择。

感谢所有帮助!谢谢你的时间!

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
union 
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
union 
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC 

答案 1 :(得分:1)

尝试此查询

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