sten
sten

Reputation: 7476

Sort the result according to the ARRAY elements?

I have the following query :

 SELECT id,word FROM map
 WHERE id::integer in (SELECT unnest(ary) FROM abc WHERE id = 11)

the problem is that the result comes in random order.

What I want is the result to come in the order defined by the content of ARRAY "ary"

How do I do that ?

Upvotes: 0

Views: 35

Answers (1)

msanzal
msanzal

Reputation: 119

I would unnest first and with that order given, would join the other tables on the id column:

SELECT
     id,
     word
FROM (
     SELECT
         unnest(ary) as id
     FROM
         abc
     WHERE
         id = 11
) a JOIN map
USING
     (id)

Upvotes: 1

Related Questions