我尝试计算t2子集中有多少新元组与t1相比
SELECT
COUNT(t2.id)
FROM (
(SELECT id, col1 FROM t2 WHERE col2=0 AND col3=0) AS t
LEFT OUTER JOIN
t1
ON
t.id=t1.id
)
WHERE
t1.id IS NULL;
子集由
定义(SELECT id, col1 FROM t2 WHERE col2=0 AND col3=0) AS t
但上述程序似乎不起作用,发出错误。
答案 0 :(得分:2)
无需在FROM
中附上()
条款。您在汇总t2.id
中引用了COUNT()
,但您的SELECT
列表只会从封装t.id
的子查询中生成t2
。此版本解决了您的错误来源:
SELECT
COUNT(t.id) AS idcount
FROM
(SELECT id, col1 FROM t2 WHERE col2=0 AND col3=0) AS t
LEFT OUTER JOIN t1 ON t.id = t1.id
WHERE t1.id IS NULL
由于您的子查询实际上非常简单,我认为根本没有必要。整个过程可以使用LEFT JOIN
:
SELECT
/* The equivalent of COUNT(*) in this context */
COUNT(t2.id) AS idcount
FROM
t2
LEFT OUTER JOIN t1 ON t2.id = t1.id
WHERE
t1.id IS NULL
AND (t2.col2 = 0 AND t2.col3 = 0)
答案 1 :(得分:1)
答案 2 :(得分:1)
问题在于别名。你有:
select count(t2.id)
但是,t2是在子查询中定义的,因此它超出了范围。
你想:
select count(t.id)