2011-04-13 88 views
4
---------- 
samples 
---------- 
SamplesID 
stylenumber 
stylename 
status 

----------- 
samples_details 
----------- 
Samples_Details_ID 
SamplesID 
CustomerName 
date_out 
date_returned 
updated (timestamp) 
status 

------------ 
samples_pictures 
------------ 
SamplesPicID 
SamplesID 

想寫一個查詢,讓我從樣本中的所有行,從samples_pictures所有比賽(如果有的話),並從samples_details最近的記錄(如果有的話)。所以兩個左外連接,但只檢索最近的記錄。MAX條件加入

什麼是不工作:

SELECT samples.* 
    , samples_pictures.SamplesPicID 
    , CustomerName 
    , date_out 
    , date_returned 
    , updated 
    , samples_details.status as txn_status 
FROM samples 
    LEFT OUTER JOIN 
     (SELECT Samples_Details_ID 
      , samples_details.status as txn_status 
      , MAX(updated) as MaxUpdated 
     FROM samples_details 
     GROUP BY Samples_Details_ID 
    ) AS MaxTable 
     ON MaxTable.SamplesID = samples.SamplesID 
    LEFT OUTER JOIN samples_pictures 
     ON samples.SamplesID = samples_pictures.SamplesID 

任何援助將不勝感激!

+0

反向MaxTable.SamplesID和samples.samplesId? – Mikeb 2011-04-13 19:02:27

回答

1

在你的select語句中沒有SamplesIDMaxTable。應該是:

SELECT samples.*, samples_pictures.SamplesPicID, CustomerName, date_out, 
date_returned, updated, samples_details.status as txn_status 
FROM samples 
    LEFT OUTER JOIN (SELECT SamplesID, Samples_Details_ID, samples_details.status as txn_status, 
    MAX(updated) as MaxUpdated FROM samples_details GROUP BY Samples_Details_ID) 
    AS MaxTable ON MaxTable.SamplesID = samples.SamplesID 
LEFT OUTER JOIN samples_pictures ON samples.SamplesID = samples_pictures.SamplesID 
1

原始查詢仍然選擇從samples_details每個記錄。

您必須創建一個子查詢來篩選出具有最大時間戳記的記錄,並將其與samples_details表加回來以獲取其他列。

SELECT * 
FROM  samples s 
     LEFT OUTER JOIN sample_details sd ON sd.SamplesID = s.SamplesID 
     INNER JOIN (
      SELECT SamplesID 
        , MAX(update) AS Update 
      FROM  samples_details 
      GROUP BY 
        SamplesID 
     ) sdm ON sdm.SamplesID = sd.SamplesID 
     LEFT OUTER JOIN samples_pictures sp ON sp.SamplesID = s.SamplesID 
+0

完美,謝謝@Lieven和@dgilland,正是我需要的! – TjJunior 2011-04-13 19:28:34

+0

@TJJunior,如果答案是正確的,你可以將其標記爲這樣,並獎勵那些幫助你的志願者,以表示讚賞。如果我們的答案都是正確的(就像你說的那樣),「dgilland」首先回答。受到尊重的答案應該交給他。 – 2011-04-13 19:50:21

0
SELECT samples.* 
    , samples_pictures.SamplesPicID 
    , CustomerName 
    , date_out 
    , date_returned 
    , updated 
    , txn_status 
FROM samples 
    LEFT OUTER JOIN 
     (SELECT Samples_ID 
      , status as txn_status 
     FROM samples_details 
      INNER JOIN 
       (SELECT Samples_ID, 
         Samples_Details_ID 
        , MAX(updated) as MaxUpdated 
       FROM samples_details 
       GROUP BY Samples_ID, Samples_Details_ID 
      ) AS MaxTable 
       ON MaxTable.Samples_ID = samples.SamplesID 
       AND MaxTable.Samples_Details_ID = samples.Samples_Details_ID 
    ) AS MaxJoin 
    LEFT OUTER JOIN samples_pictures 
     ON samples.SamplesID = samples_pictures.SamplesID 
1
SELECT samples.* 
    , samples_pictures.SamplesPicID 
    , CustomerName 
    , date_out 
    , date_returned 
    , updated 
    , samples_details.status as txn_status 
FROM samples 
    LEFT OUTER JOIN samples_details 
     ON samples.SamplesID = samples_details.SamplesID 
     AND samples_details.updated = 
     (SELECT MAX(updated) 
     FROM samples_details 
      WHERE SamplesID = samples.SamplesID) 
    ) 
    LEFT OUTER JOIN samples_pictures 
     ON samples.SamplesID = samples_pictures.SamplesID 
+0

救了我幾個小時:)好吧,也許只是十分鐘......但是與我打算做的事情相比,這是非常聰明的!謝謝 – 2014-05-05 12:28:02