简体   繁体   English

PostgreSQL-ORDER BY random()但也按ID排序?

[英]Postgresql - ORDER BY random() but also sort by id?

is it possible to order by random, and then order by an id? 是否可以随机订购,然后按ID订购?

In other words - can I tell to postgresql that it should return 10 random elements, but also give it out by sorting by the id column? 换句话说-我可以告诉postgresql它应该返回10个随机元素,但也可以通过按id列进行排序来给出它吗?

Right now it seems I can do ORDER BY random() and that works, but I get a wild, unsorted result, so I now want to sort that result by the main id it uses, but I seem to be unable to apply to ORDER BY statements. 现在看来我可以执行ORDER BY random()了,但是我得到了一个疯狂的,未排序的结果,所以我现在想按其使用的主要ID对该结果进行排序,但是我似乎无法将其应用于ORDER BY语句。

Normally I would sort by doing this: 通常我会这样做:

ORDER BY id 按ID订购

But this does not work if I already order by random... 但是,如果我已经随机订购,这将不起作用...

Maybe like this? 也许是这样吗?

select * from
  (select * from ...
    order by random()
    limit 10
  )
order by id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM