如何在mysql中从同一个表中选择具有不同状态的数据?

时间:2012-11-07 12:09:58

标签: mysql

我有一张像my table这样的表。

我想选择随机10个非花哨的suite_number 和随机10花式suite_number。现在我正在使用两个查询来执行此操作。  我可以用一个查询来做吗? 如何在mysql中以不同状态从同一个表中选择数据?

2 个答案:

答案 0 :(得分:1)

这个怎么样:

( SELECT suite_number 
    FROM tbl_name
   WHERE is_fancy = 0
ORDER BY RAND()
   LIMIT 10
) UNION ALL
( SELECT suite_number 
    FROM tbl_name
   WHERE is_fancy = 1
ORDER BY RAND()
   LIMIT 10)
ORDER BY RAND()

最后一行ORDER BY RAND()是可选的。

答案 1 :(得分:1)

SELECT suite_number
FROM my_table
WHERE is_fancy = '1'
ORDER BY RAND()
LIMIT 10
UNION
SELECT suite_number
FROM my_table
WHERE is_fancy = '0'
ORDER BY RAND()
LIMIT 10