简体   繁体   English

此SQL查询有什么问题

[英]What wrong with this SQL Query

I need to find what bad with this SQL: 我需要找出此SQL有什么不好的地方:

SELECT DepartmentName, COUNT(*) 
FROM employee,department 
WHERE employee.DepartmentID = department.DepartmentID 
GROUP BY DepartmentName
WHERE COUNT(*)>1

I think the problem in COUNT(*) beacause it return the count of all rows. 我认为COUNT(*)的问题是因为它返回所有行的计数。 So how I think the property sql will be without where statement. 因此,我认为没有where语句的sql属性将如何。 Help me I not good in SQL. 帮帮我,我不太擅长SQL。

You have to use having cluase instead of where at the end of the statement: 你必须使用具有 cluase,而不是展示在声明的结尾:

SELECT DepartmentName, COUNT(*) 
FROM employee,department 
WHERE employee.DepartmentID = department.DepartmentID 
GROUP BY DepartmentName
having COUNT(*)>1
SELECT DepartmentName, COUNT(*) AS count
FROM employee,department 
WHERE employee.DepartmentID = department.DepartmentID 
GROUP BY DepartmentName
HAVING count>1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM