0

我有一张这样的桌子我的桌子

我想选择随机的 10 个非花式suite_number 和随机的 10 个花式suite_number。现在我使用两个查询来做到这一点。我可以用一个查询来完成吗?如何从mysql中不同状态的同一张表中选择数据?

4

2 回答 2

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()是可选的。

于 2012-11-07T12:19:27.427 回答
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
于 2012-11-07T12:20:39.267 回答