gpt4 book ai didi

MYSQL:如何获取table1的具体数据以及获取table2和table3的最新数据

转载 作者:行者123 更新时间:2023-11-29 21:30:03 24 4
gpt4 key购买 nike

表 1:员工表

+------------+----------+-----------+-------+-------+
| 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:员工事件表

+------------+------------+---------------+-------+
| 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 如何假设 tblemployee 链接到 tblemployeeactivity 同样适用于 tblemployeetblbadge,但是 tblbadgetblemployeeactivity 在 SQL 中没有链接在一起。

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

PS. Also I have to filter AttritionDate by Year and Month

最佳答案

此查询可以生成所需的结果:

SELECT e.employeeid, e.lastname, e.firstname, e.id_sc, e.id_es,
q1.badgeno, q1.deact, q1.id_rem, q1.id_s,
q2.adate, q2.attritiondate, q2.id_at
FROM tblemployee e
INNER JOIN
(SELECT b.badgeno, b.employeeid, MAX(b.deactivation) AS deact, b.id_rem, b.id_s
FROM tblbadge b
GROUP BY b.employeeid) q1
ON q1.employeeid = e.employeeid
INNER JOIN
(SELECT a.employeeid, MAX(a.adate) AS adate, a.attritiondate, a.id_at
FROM tblemployeeactivity a
GROUP BY a.employeeid) q2
ON e.employeeid = q2.employeeid

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

关于MYSQL:如何获取table1的具体数据以及获取table2和table3的最新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com