1

我在 MySQL 查询中加入了几个表,它们通过员工 ID 号链接。

ID 号在表 A 和 B 中是唯一的,但在表 C 中有多个行,每个条目的日期(存储为字符串)是唯一的。

我需要将表 C 和 LIMIT 表 C 中每个员工 ID 号的最近日期标识为每个 ID 号的一个条目(显然是最近的)。

我不确定如何将每个员工 ID 号的 LIMIT 设置为 1。

感谢您的帮助。

4

2 回答 2

2

下面的查询使用子查询来获取每个on 的每个最近日期EmployeeIDtableC

SELECT  a.*, b.*, c.*
FROM    tableA a
        INNER JOIN tableB b 
            ON a.EmployeeID = b.EmployeeID
        INNER JOIN tableC c
            ON a.EmployeeID = c.EmployeeID
        INNER JOIN
        (
            SELECT  EmployeeID, MAX(date) max_date
            FROM    tableC
            GROUP   BY  EmployeeID
        ) d ON  c.EmployeeID = d.EmployeeID AND
                c.Date = d.max_date
于 2013-03-09T22:03:15.007 回答
0

From what I understood on your question:

select a.*, b.*, max(c.date) from A
     inner join B on A.id=B.id
     inner join C on A.id=C.id
     group by A.id

This would group the records by the ID of A. That is, you would get a row in the response for each of the rows in A (because they are uniquely identified by that ID). Same thing happens with B, as they have the same ID. And then, in C, you have multiple records per record in A, so grouping them and getting the max of the date will just return one date of C per A (the max of them in each case).

于 2013-03-09T23:06:30.353 回答