PostgreSQL:在 JOIN 条件中不允许使用 Set-retuning 函数
PostgreSQL: Set-retuning functions are not allowed in JOIN conditions
我有两个 table 需要加入。一个 table(称之为 table A)在名为“ProductCode”的字段中包含产品的字母代码。另一个 table (table B) 包含相同的字母代码,并附有一堆数字,所以我使用 REGEXP_MATCHES(B.ProductCode,'([ A-Za-z])','g'))[1]。然后我尝试像这样加入两个 table:
select * from A
inner join B on A.ProductCode = REGEXP_MATCHES(B.ProductCode,'([A-Za-z])','g'))[1]
例如:
Table A:
----------------------
Product Code | Sales
----------------------
A | 100
B | 200
Table B:
---------------------
Product Code | Region
---------------------
A234 | Midwest
B543 | Southwest
上述连接的预期结果是:
------------------------------
Product Code | Sales | Region
----------------------------
A | 100 | Midwest
B | 200 | Southwest
但我收到错误消息:'Set-returning functions are not allowed in JOIN conditions'。
我知道 REGEXP_MATCHES returns 是一个数组,但我正在提取该数组 ([1]) 的一个元素,所以它应该不再是一个集合?不知道如何解决它。感谢您的任何建议。
对于此示例数据,正则表达式似乎有点过分了。您可以只使用 substring()
,或 like
:
select a.*, b.region
from a
inner join b on b.product_code like a.product_code || '%'
我有两个 table 需要加入。一个 table(称之为 table A)在名为“ProductCode”的字段中包含产品的字母代码。另一个 table (table B) 包含相同的字母代码,并附有一堆数字,所以我使用 REGEXP_MATCHES(B.ProductCode,'([ A-Za-z])','g'))[1]。然后我尝试像这样加入两个 table:
select * from A
inner join B on A.ProductCode = REGEXP_MATCHES(B.ProductCode,'([A-Za-z])','g'))[1]
例如:
Table A:
----------------------
Product Code | Sales
----------------------
A | 100
B | 200
Table B:
---------------------
Product Code | Region
---------------------
A234 | Midwest
B543 | Southwest
上述连接的预期结果是:
------------------------------
Product Code | Sales | Region
----------------------------
A | 100 | Midwest
B | 200 | Southwest
但我收到错误消息:'Set-returning functions are not allowed in JOIN conditions'。
我知道 REGEXP_MATCHES returns 是一个数组,但我正在提取该数组 ([1]) 的一个元素,所以它应该不再是一个集合?不知道如何解决它。感谢您的任何建议。
对于此示例数据,正则表达式似乎有点过分了。您可以只使用 substring()
,或 like
:
select a.*, b.region
from a
inner join b on b.product_code like a.product_code || '%'