简体   繁体   English

如何选择SQL中列中具有最大值的所有记录?

[英]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.

相关问题 SQL - 如何选择具有最大值列的行(+ group by) - SQL - How to select a row having a column with max value (+ group by) SQL - 如何选择具有最大值列的行 - SQL - How to select a row having a column with max value SQL DB2:需要 select 属于客户的所有记录在 B 列中具有唯一值(值 = P) - SQL DB2: Need to select all records belonging to a client having a unique value in column B (value = P) 如何在 SQL 中 select 属于一列 MAX() 值的所有列? - How do I select all columns that belong to one columns MAX() value in SQL? SQL:如何在 Redshift 中选择具有最大值的列? - SQL: How to select COLUMN with max value in Redshift? SQL:我如何在 select 的所有列中至少有 2 个具有特定值的另一列? [使用 mariadb] - SQL: How do I select all of a column that have at least 2 of another column of a specific value? [using mariadb] 我如何只获取列 SQL 的最大值 - How do i get only the max value of a column SQL 如果其他三个字段中的两个相同,如何从id列中选择具有max的记录 - How do I select records with max from id column if two of three other fields are identical 如何选择具有最大值的列和分组依据的行 - How to select a row having a column with max value with a group by SQL SELECT列上具有MAX值的行,并返回所有列 - SQL SELECT rows with MAX value on a column and returns all columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM