MYSQL:如何在table1上获取特定数据并获取table2和table3的最新数据

时间:2016-02-11 02:03:20

标签: mysql sql

表1:tblemployee

+------------+----------+-----------+-------+-------+
| EmployeeID | LastName | FirstName | ID_SC | ID_ES |
+------------+----------+-----------+-------+-------+
| 100000000  | Vallente | Rhea Mae  | 7     | 2     |
+------------+----------+-----------+-------+-------+
| 100000001  | Margallo | Matt      | 7     | 2     |
+------------+----------+-----------+-------+-------+

表2:tblbadge

+---------+------------+--------------+--------+------+
| BadgeNo | EmployeeID | Deactivation | ID_Rem | ID_S |
+---------+------------+--------------+--------+------+
| 1111111 | 100000001  | 2015-01-02   | 9      | 2    |
+---------+------------+--------------+--------+------+
| 2222222 | 100000001  | 2014-01-02   | 9      | 2    |
+---------+------------+--------------+--------+------+

表3:tblemployeeactivity

+------------+------------+---------------+-------+
| EmployeeID | ADate      | AttritionDate | ID_AT |
+------------+------------+---------------+-------+
| 100000001  | 2015-01-01 | 2015-01-02    | 1     |
+------------+------------+---------------+-------+
| 100000001  | 2014-01-01 | 2014-01-02    | 1     |
+------------+------------+---------------+-------+

输出计划:

+------------+----------+-----------+-------+-------+---------+--------------+--------+------+------------+---------------+-------+
| EmployeeID | LastName | FirstName | ID_SC | ID_ES | BadgeNo | Deactivation | ID_Rem | ID_S | ADate      | AttritionDate | ID_AT |
+------------+----------+-----------+-------+-------+---------+--------------+--------+------+-----------------------+----+-------+
| 100000001  | Margallo | Matt      | 7     | 2     | 1111111 | 2015-01-02   | 9      | 2    | 2015-01-01 | 2015-01-02    | 1     |
+------------+----------+-----------+-------+-------+---------+--------------+--------+------+------------+---------------+-------+

问题:一个查询中最新的BadgeNoAttritionDate如何假定tblemployeetblemployeeactivity相关联,tblemployeetblbadge相同{1}}但是tblbadgetblemployeeactivity在SQL中没有链接在一起。

我必须在tblemployee上获取员工的完整详细信息并获取每个表格的最新数据(tblbadge& tblemployeeactivity),请参阅输出计划,TIA。

  

PS。另外,我必须按AttritionDateYear

过滤Month

1 个答案:

答案 0 :(得分:0)

此查询可用于生成所需结果:

{{1}}

SQLFiddle:http://sqlfiddle.com/#!9/64a64a/17