Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用特定值随机显示表中的数据_Sql - Fatal编程技术网

Sql 如何使用特定值随机显示表中的数据

Sql 如何使用特定值随机显示表中的数据,sql,Sql,我想从我的表中随机抽取5个数据,但我希望橙色的值总是显示在这个随机值中,橙色也与它们一起随机排列,sow如何做到这一点 select fruits from table ORDER BY rand() limit 5; 您可以单独选择橙色: (select fruits from table where name = 'orange') union all (select fruits from table where name <> 'orange' order by rand(

我想从我的表中随机抽取5个数据,但我希望橙色的值总是显示在这个随机值中,橙色也与它们一起随机排列,sow如何做到这一点

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

您可以单独选择橙色:

(select fruits from table where name = 'orange')
union all
(select fruits from table where name <> 'orange' order by rand() limit 4)
order by rand()
您可以使用以下方法执行此操作:

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

谢谢兄弟,但我想让我的橙色单词olso rand和他们一起@Salman Athe second rand改变它的位置。用你正在使用的数据库标记你的问题。