如何从表中随机显示具有特定值的数据

时间:2018-12-25 20:00:02

标签: sql

我希望我从表中随机抽取5个数据,但我希望值“ orange”始终显示在该随机数中,而“ orange”也要与它们相乘,那么该怎么做呢?

select fruits from table ORDER BY rand() limit 5;

2 个答案:

答案 0 :(得分:1)

您可以单独选择橙色:

(select fruits from table where name = 'orange')
union all
(select fruits from table where name <> 'orange' order by rand() limit 4)
order by rand()

答案 1 :(得分:0)

您可以使用:

select t.fruits
from table t
order by (case when t.fruit = 'orange' then 1 else 2 end),
      rand()
limit 5;