mariadb 计算由带有JOIN的嵌套SQL select语句生成的行数

2w3kk1z5  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(120)

我有以下SQL语句。

SELECT COUNT(a.id), a.project_id, p.is_recommended 
FROM assessments a 
INNER JOIN projects p ON p.id=a.project_id 
WHERE p.is_recommended = 1 
GROUP BY project_id 
HAVING COUNT(a.id) >= 3

我想计算一下结果的行数。
如果没有INNER JOIN,我所要做的就是这样的事情...

SELECT * FROM
   (SELECT COUNT(id), project_id
    FROM assessments 
    GROUP BY project_id 
    HAVING COUNT(id) >= 3) assessments

但是,如果我的语句包含JOIN,我该如何计算结果行数?我应该使用什么语法?

8e2ybdfx

8e2ybdfx1#

SELECT COUNT(*)
FROM (SELECT COUNT(a.id), a.project_id, p.is_recommended
    FROM assessments a
    INNER JOIN projects p ON p.id=a.project_id
    WHERE p.is_recommended = 1
    GROUP BY project_id
    HAVING COUNT(a.id) >= 3
    ) AS counts

相关问题