赞
踩
使用过 pgsql查询时,以下场景:
select * from test where id in (3,1,2);
查询后返回的结果很难是按照id 为3,1,2的顺序返回的,那么有些场景是需要保证顺序不变返回的,如何实现呢?
select id,
actors,
name,
avatar,
period,
score
from movie
where id = any (array [1, 5, 4, 2, 3]::bigint[])
order by array_positions(array [1, 5, 4, 2, 3]::bigint[], id::bigint)
如果有类型问题可能会报错,可以强转:
order by array_positions(array [1, 5, 4, 2, 3], cast(id as INTERGER))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。