2016-02-11 45 views
0

表1的最新數據:tblemployeeMYSQL:如何獲得table1的具體數據和獲得對錶2和表3

+------------+----------+-----------+-------+-------+ 
| 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假設tblemployee鏈接到tblemployeeactivitytblemployeetblbadge然而tblbadgetblemployeeactivity不鏈接在一起也一樣在SQL中。

我得對tblemployee員工的全部細節,並獲取每個表(tblbadge & tblemployeeactivity)的最新數據,請參見輸出計劃,TIA。

PS。我也有過濾AttritionDateYearMonth

+0

'選擇一個*,b.BadgeNo,B。取消激活,b.ID_Rem,b.ID_S,c.ADate,c.AttritionDate,c.ID_AT FROM tblemployee a, (SELECT BadgeNo,Deactivation,ID_Rem,ID_S FROM tblbadge WHERE EmployeeID = a.EmployeeID ORDER BY Deactivation DESC LIMIT 1 )b , (SELECT ADate,AttritionDate,ID_AT FROM tblemployeeactivity WHERE EmployeeID = a.EmployeeID ORDER BY AttritionDate DESC LIMIT 1)c'可能不是最好的方法,但給一個鏡頭。 – Matt

+0

SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'a.EmployeeID' 'a.EmpoyeeID'在where子句中未被識別 –

回答

0

此查詢的工作產生期望的結果:

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

+0

它的工作原理,它實際上保存我的a **,非常感謝你,我只是調整一些數據過濾的SQL。 –