[英]How do I select all records having the max value in a column in SQL?
Suppose I have the following Database:假设我有以下数据库:
Create table Employee (emp_id int, emp_name varchar(20), staff int);
Insert into Employee values(101,"John", 3);
Insert into Employee values(102,"Mary", 3);
Insert into Employee values(103,"Smith",2);
Insert into Employee values(104,"Bill", 2);
Insert into Employee values(105,"Kelly", 1);
I want it to select print John and Mary, because they have the highest staff, ie 3. Could anyone please shed some light?我希望它选择印刷版约翰和玛丽,因为他们的员工人数最多,即 3。有人能解释一下吗?
I have tried doing:我试过这样做:
select emp_name from Employee having staff = max(staff);
but i keep getting an error!但我不断收到错误!
You can use a subquery that returns the max staff of the table:您可以使用返回表的最大员工数的子查询:
select e.* from Employee e
where e.staff = (select max(staff) from Employee)
= (SELECT MAX(staff) FROM Employee)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.