基于特定值的Postgresql排序依据
我有一个名为基于特定值的Postgresql排序依据,sql,postgresql,Sql,Postgresql,我有一个名为item\u priority 其中包含以下值“重要”、“超级重要”、“重要1”、“重要2” 我想对结果进行排序,排序方式如下所示: 'important', 'super_important', 'important_1', 'important_2' 按顺序出现 这是我的查询,目前,important_2出现在important_1之前 SELECT * from main_table where product_id = '200' ORDER BY col1, col2,
item\u priority
其中包含以下值“重要”、“超级重要”、“重要1”、“重要2”
我想对结果进行排序,排序方式如下所示:
'important', 'super_important', 'important_1', 'important_2'
按顺序出现
这是我的查询,目前,important_2
出现在important_1
之前
SELECT * from
main_table
where product_id = '200'
ORDER BY col1, col2, CASE WHEN item_priority ~ '^[a-zA-Z]' THEN 1 WHEN WHEN item_priority~ '^[0-9]' THEN 2 END, item_priority desc, col3
orderby支持任意表达式。将执行您要查找的操作,使最后一个子句
按数组位置排序(['important'、'super\u important'、'important\u 1'、'important\u 2'],item\u priority)
ORDER BY支持任意表达式。将完成您要查找的内容,使最后一个子句按数组位置排序(['important'、'super\u important'、'important\u 1'、'important\u 2'],item\u priority)按数组排序将特别慢
只需使用这样一个有价值的案例:
SELECT *
FROM main_table
WHERE product_id = '200'
ORDER BY col1, col2,
CASE item_priority
WHEN 'important' THEN 1
WHEN 'super_important' THEN 2
WHEN 'important_1' THEN 3
WHEN 'important_2' THEN 4
END;
事实上,有值大小写比任何其他形式的排序都要快…按数组排序会特别慢 只需使用这样一个有价值的案例:
SELECT *
FROM main_table
WHERE product_id = '200'
ORDER BY col1, col2,
CASE item_priority
WHEN 'important' THEN 1
WHEN 'super_important' THEN 2
WHEN 'important_1' THEN 3
WHEN 'important_2' THEN 4
END;
事实上,有值大小写比任何其他形式的排序都要快…我遇到了语法问题:<代码>第45行或其附近的语法错误:按数组位置排序([我在这之前添加了
array
,现在我得到提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。这就是我最后得到的按数组位置排序(array['important'、'super_important'、'important_1'、'important_2'],item_priority),col1、col2、col3
我遇到了语法问题。第45行或附近的语法错误:按数组位置排序([
在此之前,我添加了数组,现在我得到了提示:没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换。这就是我最后得到的按数组位置排序的结果(数组['important'、'super\u important'、'important\u 1'、'important\u 2'],item\u priority),col1,col2,col3
不幸的是,这并没有真正改变顺序。也就是说,重要的节目排在任何事情之前。不幸的是,这并没有真正改变顺序。也就是说,重要的节目排在任何事情之前。