English 中文(简体)
在每个组中筛选最大值,谁能告诉我我的SQL语句哪里错了?[副本]
原标题:filtering max value in each group, who can tell me where is my SQL statement wrong? [duplicate]
  • 时间:2023-07-14 14:23:35
  •  标签:
  • sql
  • mysql
This question already has answers here:
Closed 11 hours ago.

This post was edited and submitted for review 11 hours ago.

我想筛选出每个部门薪水最高的记录。如果一个部门中有多条记录符合此条件,请将其全部保留。然后我编写SQL如下:

SELECT id, name, salary, departmentId
  FROM Employee
  GROUP BY departmentId
  HAVING salary = MAX(salary)

但它不能正常工作。

员工表:

+----+-------+--------+--------------+
| id | name  | salary | departmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

id is the primary key column for this table.

执行的结果是:

| id | name  | salary | departmentId |
| -- | ----- | ------ | ------------ |
| 3  | Henry | 80000  | 2            |

但我预期的结果是这样的:

| id | name  | salary | departmentId |
| -- | ----- | ------ | ------------ |
| 2  | Jim   | 90000  | 1            |
| -- | ----- | ------ | ------------ |
| 3  | Henry | 80000  | 2            |
| -- | ----- | ------ | ------------ |
| 5  | Max   | 90000  | 1            |

I want to know why I can t get the result that I expect? what s wrong with my SQL? I know how to write the correct SQL statement now. I just want to know what exactly is wrong with my SQL statement. Analysis of cause is more important for me.

问题回答

您可以使用子查询来查找部门的最高工资。

SELECT 
  * 
FROM
  `Employee` e 
WHERE e.`salary` = 
  (SELECT 
    MAX(e1.`salary`) 
  FROM
    `Employee` e1 
  WHERE e1.`departmentId` = e.`departmentId`)

或者

SELECT e.id, e.name, e.salary, e.departmentId
FROM Employee e
INNER JOIN (
  SELECT departmentId, MAX(salary) AS max_salary
  FROM Employee
  GROUP BY departmentId
) subquery
ON e.departmentId = subquery.departmentId AND e.salary = subquery.max_salary;

This answer also works for MySQL 5.6 If you are using MySQL 8.X, you have your answer already in the comment section

select 
se.id, 
se.name, 
se.salary, 
se.department_id 
from stack_employees as se
join (
    select 
    department_id, 
    max(salary) as max_salary 
    from stack_employees
    group by department_id) di
on se.department_id = di.department_id
and se.salary = di.max_salary;

这将为您提供所需的输出。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签