有2个名为
的表学生
校园
我想找到超过4名学生的校园,其中包括camName,camID,(学生人数)
这是我到目前为止所得到的
SELECT
students.camID, campus.camName, SUM(students.stuID) as [count]
FROM
students
JOIN
campus ON campus.camID = students.camID
WHERE
[count] > 3
GROUP BY
students.camID, campus.camName
ORDER BY
[count]
所有这一切都让我感到错误是“无效的comlumn名称'计数'。
答案 0 :(得分:2)
您不能在WHERE
子句中使用列别名,因为在甚至创建别名之前会对评估WHERE
子句。您也不能在HAVING
子句中使用别名。
SELECT students.camID, campus.camName, COUNT(students.stuID) as studentCount
FROM students
JOIN campus
ON campus.camID = students.camID
GROUP BY students.camID, campus.camName
HAVING COUNT(students.stuID) > 3
ORDER BY studentCount
答案 1 :(得分:0)
SELECT [t0].* FROM campus AS [t0]
INNER JOIN (SELECT COUNT(*) AS [value], [t1].camID
FROM students AS [t1]
GROUP BY [t1].camID )
AS [t2] ON [t0].camID = [t2].camID
WHERE [t2].[value] > 3
答案 2 :(得分:0)
第一批SQL产品不支持派生表,因此发明了HAVING
。但是现在我们确实有派生表,所以我们不再需要HAVING
,实际上它可能会导致混淆(注意遗留功能永远不会从SQL标准中删除):
SELECT *
FROM (
SELECT students.camID, campus.camName,
SUM(students.stuID) as [count]
FROM students
JOIN campus ON campus.camID = students.camID
GROUP
BY students.camID, campus.camName
) AS DT1
WHERE [count] > 3
ORDER
BY [count]