我正在写一个sql查询,但仍然有相同的错误,不知道如何解决它。

select k.klantnr, aantal
from klanten k INNER JOIN(
select d.klantnr, count(d.reisnr) as aantal
from deelnames d
where k.klantnr = d.klantnr
group by d.klantnr) as adeelnames USING(klantnr)

错误:
There is an entry for table "k", but it cannot be referenced from this part of the query.

最佳答案

子查询中不需要correlation子句:

select klantnr, aantal
from klanten k INNER JOIN
     (select d.klantnr, count(d.reisnr) as aantal
      from deelnames d
      group by d.klantnr
    ) adeelnames
    USING (klantnr);

USING子句可以满足您的所有需要。注意在k.中删除了SELECT
此外,如果希望计数为0,则可能需要LEFT JOIN

关于sql - 表“k”有一个条目,但不能在查询的此部分中引用它。子查询SQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32075104/

10-15 21:29