2014-04-30 129 views
-1

我有一個系統可以請求驅動程序。 如果不止一位司機請求同一行程,它會轉到首先與我們一起工作的司機。如果他們在同一天開始,我需要這個生日,以便最老的一個獲得參觀。MySQL:多表連接

Table 1: Data of the employees SEN = Date of Entry, BIR = Birthday

Table 2: Tours

Table 3: Requests with the tourid as well as the employeeid

這裏是我的查詢:

SELECT T.*, ER.SEN, requestcnt FROM tours T LEFT OUTER JOIN ( SELECT 
R.tourid, count(R.requestid) AS requestcnt, MIN(E.SEN) AS SEN 
     FROM requests R 
     INNER JOIN employees E ON E.employeeid = R.employeeid WHERE Funktion = 'XY' 
     GROUP by R.tourid 
     ) SR ON ER.tourid = U.tourid 

如何獲取到該遊最低資歷相匹配的員工的生日。如果我只是將生日字段添加到秒選語句中,即使他/她沒有請求旅程,我也會獲得總體最低年齡的員工的生日。

我們的目標是將所有內容都放在一個查詢中,因爲導遊的數量很大。此查詢適用於概覽頁面,因此我可以根據行程的可用性顯示錶情符號。

感謝您的任何建議!

+2

請樣本數據,預期的效果編輯你的問題,並解釋您的查詢無法正常工作的原因。 –

回答

0

可能對請求和員工進行另一次連接。

事情是這樣的: -

SELECT T.*, ER2.SEN, ER2.BIR, requestcnt 
FROM tours T 
LEFT OUTER JOIN 
(
    SELECT R.tourid, count(R.requestid) AS requestcnt, MIN(E.SEN) AS SEN 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    GROUP by R.tourid 
) ER 
ON ER.tourid = U.tourid 
LEFT OUTER JOIN 
(
    SELECT R.tourid, E.SEN, MIN(E.BIR) AS BIR 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    GROUP by R.tourid, E.SEN 
) ER2 
ON ER2.tourid = U.tourid 
AND ER.SEN = ER2.SEN 

編輯 - 對於修訂後的要求

獲取任何東西的3是尷尬的,因爲你不能真正使用MIN/MAX功能得到它(用很多努力,你可以通過使用它們來找到並排除min,然後再用這個結果等等)。對於連接的子查詢,您無法輕鬆使用限制,因爲它可以處理查詢的整體結果,而不是每個組。

2種方式我可以想到我的頭頂。首先將一個序列號添加到子查詢中,該子查詢將按照資歷順序得到結果,這將重新設置三元組更改。

像這樣(未測試): -

SELECT T.*, ER2.SEN, ER2.BIR, requestcnt 
FROM tours T 
LEFT OUTER JOIN 
(
    SELECT R.tourid, count(R.requestid) AS requestcnt 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    GROUP by R.tourid 
) ER 
ON ER.tourid = T.tourid 
LEFT OUTER JOIN 
(
    SELECT R.tourid, E.employeeid, E.SEN, E.BIR, @seq:=IF([email protected], @seq+1, 1) AS seq 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    CROSS JOIN (SELECT @seq:=0, @tourid:=0) 
    WHERE Funktion = 'XY' 
    ORDER BY R.tourid, E.SEN, E.BIR, E.employeeid 
) ER2 
ON ER2.tourid = T.tourid 
AND ER2.seq = 3 

另一種方法是使用相關子查詢的SELECT。這可能會顯着放慢的行數返回的增加: -

SELECT T.*, 
     (
      SELECT E.SEN 
      FROM requests R 
      INNER JOIN employees E ON E.employeeid = R.employeeid 
      WHERE Funktion = 'XY' 
      AND R.tourid = T.tourid 
      ORDER BY E.SEN, E.BIR 
      LIMIT 3,1 
     ), 
     (
      SELECT E.BIR 
      FROM requests R 
      INNER JOIN employees E ON E.employeeid = R.employeeid 
      WHERE Funktion = 'XY' 
      AND R.tourid = T.tourid 
      ORDER BY E.SEN, E.BIR 
      LIMIT 3,1 
     ), 
     requestcnt 
FROM tours T 
LEFT OUTER JOIN 
(
    SELECT R.tourid, count(R.requestid) AS requestcnt 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    GROUP by R.tourid 
) ER 
ON ER.tourid = T.tourid 

或可能在WHERE子句中(同樣未測試)

SELECT T.*, EM.SEN, EM.BIR, EM.employeeid, 
     requestcnt 
FROM tours T 
CROSS JOIN employees EM 
LEFT OUTER JOIN 
(
    SELECT R.tourid, count(R.requestid) AS requestcnt 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    GROUP by R.tourid 
) ER 
ON ER.tourid = T.tourid 
WHERE EM.employeeid = 
(
    SELECT E.employeeid 
    FROM requests R 
    INNER JOIN employees E ON E.employeeid = R.employeeid 
    WHERE Funktion = 'XY' 
    AND R.tourid = T.tourid 
    ORDER BY E.SEN, E.BIR 
    LIMIT 3,1 
) 
+0

完美!謝謝!!!! – user3549835

+0

我現在有一個問題。如果您發佈類似的請求,我需要擁有第三高級職位的員工。上面的帖子是針對最古老的(年齡和年齡)。我試圖用LIMIT修改,但我沒有找到解決方案。 感謝您的幫助! 如果有幾個員工在同一天開始和我一起工作,我需要獲得第三個最老的員工。 – user3549835

+0

提出了一些建議,但未經過測試。希望他們幫助。 – Kickstart