我有一个表,从中选择了一些值,这些值将在稍后的过程中检查是否存在于某些其他查询中。这样做的正确方法是什么?
以下是我现在的表现:
table smt:
s_id | num
1 | 2
1 | 3
2 | 2
table smtable:
id | b_id
1 | 2
2 | 3
arr_sid
应该有:{1,2}
DECLARE arr_sid INT[];
SELECT s_id
INTO arr_sid
FROM smt
GROUP BY s_id
HAVING SUM(num) <> 0;
SELECT id
INTO val
FROM smtable
WHERE b_id = pid AND id = ANY(arr_sid);
我收到此错误:
ERROR: malformed array literal: "1"
SQL state: 22P02
Detail: Array value must start with "{" or dimension information.
答案 0 :(得分:2)
只需要一个带有where子句的select语句,并在该子句中使用IN关键字和第二个select语句:
SELECT id
INTO val
FROM smtable
WHERE b_id = pid AND id IN (
SELECT s_id
FROM smt
GROUP BY s_id
HAVING SUM(num) <> 0)
不需要arr_sid
变量,并且其他数组不是标准SQL,而此查询是可移植的。