包括从内部select语句到输出的值

wmtdaxz3  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(329)

我正在尝试检查内部select语句中的总计数(*)

SELECT *
     FROM CAR
     WHERE 4 <=   ( SELECT COUNT(*) 
                    FROM CAR_DRIVER
                    WHERE CAR.C_CARKEY = CAR_DRIVER.CD_CARKEY );

我试过了 select count as total 但是它不起作用,因为变量total只在内部select语句中有效。

i7uq4tfw

i7uq4tfw1#

使用 join 因此您可以获取值:

SELECT c.*, cd.cnt
 FROM CAR c JOIN
      (SELECT cd.CD_CARKEY, COUNT(*) as cnt
       FROM CAR_DRIVER cd
       GROUP BY cd.CD_CARKEY
      ) cd
      ON c.C_CARKEY = cd.CD_CARKEY
 WHERE cnt >= 4;

相关问题