[英]MySQL sorting twice on same column
My database is like我的数据库就像
id|service_id
1 | 8
2 | 3
3 | 4
4 | 1
5 | 3
6 | 2
7 | 1
i want to sort it like rows with same service_id
came together but randomly.我想对它进行排序,就像具有相同service_id
行聚集在一起但随机。 like喜欢
id|service_id
1 | 8
4 | 1
7 | 1
2 | 3
5 | 3
6 | 2
3 | 4
means first all rows sort via service_id
then again sort via random service_id
.意味着首先所有行通过service_id
排序,然后再次通过随机service_id
排序。 I have tried SELECT * from sample_table order by service_id DESC
and tried ASC
too but it only do sort via DESC
or ASC
.我已经尝试了SELECT * from sample_table order by service_id DESC
并尝试了ASC
但它只能通过DESC
或ASC
进行排序。 I have tried order by rand(service_id)
too but it is also showing some fixed sorting.我也尝试过order by rand(service_id)
排序,但它也显示了一些固定排序。
select t.*
from your_table t
join
(
select service_id, rand() as r
from your_table
group by service_id
) tmp on t.service_id = tmp.service_id
order by tmp.r;
Basically this makes a random substitute for the service_id
and orders by that基本上这会随机替换service_id
并通过它订购
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.