Re: Problem with ORDER BY and random() ?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Problem with ORDER BY and random() ?
Дата
Msg-id 24031.1064356236@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Problem with ORDER BY and random() ?  (Jean-Francois.Doyon@CCRS.NRCan.gc.ca)
Список pgsql-general
Jean-Francois.Doyon@CCRS.NRCan.gc.ca writes:
> I'm trying to retrieve a limited number of random rows, and order them by a
> column, and am not having any luck with that last part:
> SELECT * FROM tablename ORDER BY random(), id LIMIT 10
> Returns everything more or less as expected, except for the fact that the
> results aren't sorted by "id" ...

Well, no.  You specified random() as the major sort key.  Only rows that
happened to have equal random() values would be sorted by id.

This would work:

    SELECT * FROM
      (SELECT * FROM tablename ORDER BY random() LIMIT 10) as ss
    ORDER BY id;

            regards, tom lane

В списке pgsql-general по дате отправления:

Предыдущее
От: "scott.marlowe"
Дата:
Сообщение: Re: Problem with ORDER BY and random() ?
Следующее
От: Stephan Szabo
Дата:
Сообщение: Re: Problem with ORDER BY and random() ?