Re: order by question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I agree with Scott, if your application generate IN condition, could create
> CASE too, looks like this

> SELECT id, product_id FROM your_table
>    WHERE product_id IN (6, 3, 4, 10, 7)
>     ORDER BY
>       CASE
>         WHEN product_id = 6  THEN 1
>         WHEN product_id = 3  THEN 2
>         WHEN product_id = 4  THEN 3
>         WHEN product_id = 10 THEN 4
>         WHEN product_id = 7  THEN 5
>       END;

A shorter solution would be

  ORDER BY product_id != 6,
           product_id != 3,
           product_id != 4,
           product_id != 10,
           product_id != 7,
           product_id


-- 
Sent via pgsql-admin mailing list (pgsql-admin@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin

[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux