我有以下表格:
business
id catid subcatid
---------------------
10 {1} {10,20}
20 {2} {30,40}
30 {3} {50,60,70}
cat_subcat
catid shortname parent_id bid
--------------------------------------------
1 A 10
2 B 20
3 c 30
10 x 1 10
20 y 1 10
30 z 2 20
40 w 2 20
两个表都使用 id 建立关系。我遇到的问题概述如下。这是我目前的查询:
SELECT ARRAY[category_id]::int[] from cat_subcat
where parentcategoryid IS not NULL and shortname ilike ('x,y');
我想获取输入的 shortname
的 category_id
,但我的查询没有给出正确的输出。如果我传递一个 shortname
,它将检索 category_id
,但如果我传递多个 shortname
,它将不会显示 category_id
。请告诉我如何为多个 shortname
获取 category_id
。
最佳答案
要实际使用 ILIKE
的模式匹配,您不能使用简单的 IN
expression .相反,您需要 ILIKE
ANY (...)
or ALL (...)
,取决于您想要测试 ORed 还是 ANDed:
此外,您的 ARRAY
构造函数将应用于单独的行,这似乎毫无意义。我假设你想要这个(有根据的猜测):
SELECT array_agg(catid) AS cats
FROM cat_subcat
WHERE parent_id IS NOT NULL
AND shortname ILIKE ANY ('{x,y}');
好吧,只要您不对您的模式使用通配符 (%
, _
),您就可以将其转换为:
AND lower(shortname) IN ('x','y');
但这毫无意义,因为 Postgres 在内部将其转换为:
AND lower(shortname) = ANY ('{x,y}');
.. 在评估之前。
关于postgresql - 获取超过一个短名称的类别ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993802/