左外连接处的MAX条件

时间:2011-04-13 18:57:15

标签: mysql

----------
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中的最新记录(如果有)。所以两个左外连接,但只检索最近的记录。

什么不起作用:

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

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

您的原始查询仍会从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

答案 1 :(得分:1)

SamplesID的select语句中没有MaxTable。应该是:

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

答案 2 :(得分: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

答案 3 :(得分: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