我目前正在尝试让数据库列出感兴趣的工作编号,用户标题和对兴趣描述感兴趣的用户计数。该查询的目的是在“感兴趣的用户编号。没有感兴趣的用户的兴趣应该被排除”的标题下进行计数。
(PK) = Primary Key
(FK) = Foreign Key
数据库模式如下:
Building(buildingNum(PK), Description, instname, buildName, state, postcode)
User(UNum(PK), buildingNum(FK), Surname, FirstName, initials, title)
File(FileNum(PK), title)
UserAccount(FileNum(PK)(FK), UNum(PK)(FK))
Job(JobNum(PK), id, title)
Interest(JobNum(PK)(FK), UNum(PK)(FK), Description)
到目前为止,我已经尝试了以下代码块:
select I.JobNum, U.title, count(I.description) AS 'No. Academics Interested'
from Interest I join Users U
where U.UNum = I.UNum AND I.description != null;
我正在为如何使用子查询来做到这一点而苦苦挣扎,所有我收到的都是一个错误,因为这不起作用。我不确定如何在标题下做计数(I.description),以及我应该如何做。感谢任何能帮上忙的人。
发布于 2019-05-22 12:27:47
您可能需要在此处使用GROUP BY
进行聚合:
SELECT
i.JobNum,
u.title,
COUNT(i.description) AS "No. Academics Interested"
FROM Interest i
LEFT JOIN Users u
ON u.UNum = i.UNum
GROUP BY
i.JobNum,
u.title;
注意,检查I.description != null
(实际上应该是i.description IS NOT NULL
)在这里不是必需的,因为默认情况下COUNT
函数不计算NULL
值。
https://stackoverflow.com/questions/56249237
复制相似问题