Rodrigo Salguero
Rodrigo Salguero

Reputation: 31

postgresql order by field?

I want to get the result ordered by the same order of the list, but it keeps giving errors.

select "Usuario"."nombre", "Usuario"."codSis" 
from "public"."Usuario"
where "codSis" in (6, 8)
order by field(codSis ,6,8)

this code gives mi this error

ERROR:  column "codsis" does not exist
LINE 4: order by field(codSis ,6,8)
                       ^
HINT:  Perhaps you meant to reference the column "Usuario.codSis".
SQL state: 42703
Character: 112

but when I put this

select "Usuario"."nombre", "Usuario"."codSis" 
from "public"."Usuario"
where "codSis" in (6, 8)
order by field(Usuario.codSis ,6,8)

it gives me this error

ERROR:  column "Usuario.codSis" does not exist
LINE 4: order by field("Usuario.codSis" ,6,8)
                       ^
SQL state: 42703
Character: 112

then I tried this

select "Usuario"."nombre", "Usuario"."codSis" 
from "public"."Usuario"
where "codSis" in (6, 8)
order by field("Usuario"."codSis" ,6,8)

and it gave me this error

ERROR:  function field(integer, integer, integer) does not exist
LINE 4: order by field("Usuario"."codSis" ,6,8)
                 ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 106

Upvotes: 0

Views: 1346

Answers (1)

Slava Rozhnev
Slava Rozhnev

Reputation: 10163

You query should be next:

select
  "Usuario"."nombre",
  "Usuario"."codSis" 
from "public"."Usuario"
where "codSis" in (6, 8)
order by "Usuario"."codSis" asc;

Upvotes: 1

Related Questions