----------
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
任何援助將不勝感激!
反向MaxTable.SamplesID和samples.samplesId? – Mikeb 2011-04-13 19:02:27