简体   繁体   中英

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

is it possible to order by random, and then order by an 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?

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.

Normally I would sort by doing this:

ORDER BY 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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