Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按where子句选择一个现有行顺序_Sql_Postgresql - Fatal编程技术网

Sql 按where子句选择一个现有行顺序

Sql 按where子句选择一个现有行顺序,sql,postgresql,Sql,Postgresql,我有一张这样的桌子: ID | lang --------- 1 | ru 2 | en 我想从where子句中选择一个最上面的现有行。不是按id或lang排序,而是按my where子句排序 在'en'中,lang,'ru'应该返回与lang en一起的行,因为在我的子句中,en位于第一位 其中,“de”中的lang应返回带有lang ru的行,因为不存在带有de的行 这无法正常工作,因为它是按默认顺序排序的: 从表_名称中选择*,其中lang在'en','ru'限制1中 没有默认顺序。

我有一张这样的桌子:

ID | lang
---------
1  | ru
2  | en
我想从where子句中选择一个最上面的现有行。不是按id或lang排序,而是按my where子句排序

在'en'中,lang,'ru'应该返回与lang en一起的行,因为在我的子句中,en位于第一位

其中,“de”中的lang应返回带有lang ru的行,因为不存在带有de的行

这无法正常工作,因为它是按默认顺序排序的:

从表_名称中选择*,其中lang在'en','ru'限制1中

没有默认顺序。没有ORDER BY的SQL查询将返回一个无序的结果集,并且在相同数据上运行相同查询时,顺序可能会发生变化

只需根据需要添加订单即可。例如:

order by (case lang where 'en' then 1 when 'ru' then 2 end)
或使用阵列:

where lang = any (array['en', 'ru'])
order by array_position(array['en', 'ru'], lang)

谢谢但我在尝试第二个变量时出错:函数数组_positiontext[],字符变化不会exist@Tryam . . . 下面是一个工作的例子:。再次感谢!这是我的sql编辑器中的一个问题