编辑版本
员工表
|ID | employee_id | Name |
| 1 | 123 | John Richard |
| 2 | 554 | Daniel Domingo |
教育背景
|ID | employee_id | School/institute | date graduated |
| 1 | 123 | highschool | 2007 |
| 2 | 123 | college | 2011 |
| 3 | 554 | college | 2010 |
| 4 | 554 | masteral | 2013 |
职称
|ID | employee_id | Job description |
| 1 | 123 | Free lancer |
| 2 | 554 | admin assistant |
我需要选择员工教育背景的最新日期信息
结果将是
结果查询
|ID | employee_id | Name | Job title | year_graduated | school_institute |
| 1 | 123 | John Richard | Free Lancer | 2011 | college |
| 2 | 554 | Daniel Domingo | Admin Assistant | 2013 | masteral |
答案 0 :(得分:2)
试试这个:
SELECT *
FROM (SELECT * FROM tableA ORDER BY employee_id, date_graduated DESC) A
GROUP BY employee_id
或强>
SELECT a.*
FROM tableA a
INNER JOIN (SELECT employee_id, MAX(date_graduated) maxDate
FROM tableA GROUP BY employee_id
) b ON a.employee_id = b.employee_id AND a.date_graduated = b.maxDate
答案 1 :(得分:1)
SELECT employee_id ,MAX([date graduated]) FROM Table GROUP BY [employee_id]
结果是
employee_id | date graduated
----------------------------
123 | 2011
554 | 2013
答案 2 :(得分:1)
子查询将返回每个员工的最大日期,然后您应该将此子查询与您的表一起返回完整行:
SELECT yourtable.*
FROM
yourtable INNER JOIN (SELECT employee_id, MAX(`date graduated`) max_date
FROM yourtable
GROUP BY employee_id) m
ON yourtable.employee_id = m.employee_id
AND yourtable.`date graduated` = m.max_date
答案 3 :(得分:1)
SELECT * FROM `your_table`
WHERE employee_id=123
ORDER BY date_graduated DESC LIMIT 1
员工554的
SELECT * FROM `your_table`
WHERE employee_id=554
ORDER BY date_graduated DESC LIMIT 1
答案 4 :(得分:-1)
SELECT *
FROM t
GROUP BY employee_id
HAVING date_graduated=max(date graduated)
并且您将获得所需的结果