获取MySQL的最新记录

时间:2017-01-20 12:03:40

标签: mysql max

我有这张包含员工合约记录的表格。

+----+---------------------------+
| id | start| end     | employee |
+----+---------------------------+
|  8 | 2016 | 2017    | 777      |
|  7 | 2014 | 2015    | 777      |
|  6 | 2012 | 2013    | 777      |
|  5 | 2010 | 2011    | 777      |
|  3 | 2016 | 2017    | 666      |
|  4 | 2014 | 2015    | 666      |
|  2 | 2012 | 2013    | 666      |
|  1 | 2010 | 2011    | 666      |
+----+---------------------------+

我很难获得每位员工的最新合同。

查询应该打印:

+----+-----------------+
| id | start| employee |
+----+-----------------+
|  8 | 2016 | 777      |
|  3 | 2016 | 666      |
+----+-----------------+

到目前为止我已尝试过:

SELECT 
MAX(start)
,id
,employee
FROM contract
GROUP BY employee

但这会让我 id 与记录不对应。

3 个答案:

答案 0 :(得分:1)

不要使用group by。您想要过滤行,因此请使用where - 在这种情况下使用某种子查询。

这是一种方法:

SELECT c.*
FROM contract c
WHERE c.id (SELECT MAX(c2.id)
            FROM contract c2
            WHERE c2.employee = c.employee
           );

答案 1 :(得分:1)

以下是提案:

SELECT t.*
FROM test t
WHERE t.start in (SELECT MAX(t2.start)
            FROM test t2
            group by t2.employee
           );

和SQLFiddle LINK

答案 2 :(得分:1)

创建表格/插入数据

CREATE TABLE contract
    (`id` INT, `start` INT, `end` INT, `employee` INT)
;

INSERT INTO contract
    (`id`, `start`, `end`, `employee`)
VALUES
    (8, 2016, 2017, 777),
    (7, 2014, 2015, 777),
    (6, 2012, 2013, 777),
    (5, 2010, 2011, 777),
    (3, 2016, 2017, 666),
    (4, 2014, 2015, 666),
    (2, 2012, 2013, 666),
    (1, 2010, 2011, 666)
;

使用用户变量标记每个分组员工的最高价格。

用户变量查询

SELECT
     contract.id
   , contract.start
   , contract.employee
   , (contract.employee != @employee) AS employeeMax -- Mark a new employee as Max
   , (@employee := contract.employee) AS employeeInit
  FROM
   contract
  ORDER BY
      contract.employee 
    , contract.start DESC -- DESC for MAX(start) ASC for MIN(start)

<强>结果

    id   start  employee  employeeMax  employeeInit  
------  ------  --------  -----------  --------------
     3    2016       666            1             666
     4    2014       666            0             666
     2    2012       666            0             666
     1    2010       666            0             666
     8    2016       777            1             777
     7    2014       777            0             777
     6    2012       777            0             777
     5    2010       777            0             777

完成查询

现在过滤employeeMax = 1只保留最好的价格记录。

SELECT
   contract_group.id
 , contract_group.start
 , contract_group.employee
FROM (
  SELECT
     contract.id
   , contract.start
   , contract.employee
   , (contract.employee != @employee) AS employeeMax -- Mark a new employee as Max
   , (@employee := contract.employee) AS employeeInit
  FROM
   contract
  ORDER BY
      contract.employee 
    , contract.start DESC -- DESC for MAX(start) ASC for MIN(start)
)
 AS
   contract_group
WHERE
 contract_group.employeeMax = 1 -- keep only the employee max marked records

<强>结果

    id   start  employee  
------  ------  ----------
     3    2016         666
     8    2016         777