简体   繁体   English

MySQL左连接与LIMIT 1不返回期望的结果

[英]MySQL left join with LIMIT 1 not returning desired result

I am having trouble with a SQL query; 我在执行SQL查询时遇到麻烦; I am trying to get the last service date of each 'asset'. 我正在尝试获取每个“资产”的最后服务日期。

I am trying to use a left join with a subquery with a limit to 1. 我正在尝试将左联接与子查询限制为1。

Here are my tables: 这是我的桌子:

lctn_test lctn_test

testID, cleintID
1,      34
2,      34

srvc_test srvc_test

srvcTestID, testID, serviceDate,            servicePassed
1,          1,      2018-05-19 03:23:53,    1
2,          1,      2018-05-19 11:46:49,    1
3,          2,      2018-05-19 11:47:24,    1

and here is what I have tried (as well as a few variations) 这是我尝试过的(以及一些变化)

SELECT 
    lctn.testID AS assetID, lctn.ClientID, 
    srvc_test.serviceDate, srvc_test.servicePassed
FROM 
    lctn_test AS lctn
LEFT JOIN 
    srvc_test ON lctn.testID = (SELECT srvc_test.testID
                                FROM srvc_test
                                WHERE srvc_test.testID = lctn.testID
                                ORDER BY srvc_test.serviceDate DESC
                                LIMIT 1)
WHERE 
    lctn.ClientID = 34
ORDER BY 
    assetID

What I expected to get: 我期望得到什么:

assetID,    ClientID,   serviceDate,            servicePassed
1,          34,         2018-05-19 11:46:49,    1
2,          34,         2018-05-19 11:47:24,    1

but this is what I actually get: 但这实际上是我得到的:

assetID,    ClientID,   serviceDate,            servicePassed
1,          34,         2018-05-19 03:23:53,    1
1,          34,         2018-05-19 11:46:49,    1
1,          34,         2018-05-19 11:47:24,    1
2,          34,         2018-05-19 03:23:53,    1
2,          34,         2018-05-19 11:46:49,    1
2,          34,         2018-05-19 11:47:24,    1

I am still learning SQL (mysql) and for the life of me I can't see the issue; 我仍在学习SQL(mysql),终生看不到问题。 I am betting it is a noob mistake but I just don't see it. 我敢打赌这是一个菜鸟错误,但我只是看不到它。

You have a LEFT JOIN b ON a.id = (sub-query) 您在a LEFT JOIN b ON a.id = (sub-query)a LEFT JOIN b ON a.id = (sub-query)

You should have a LEFT JOIN b ON b.id = (sub-query) 您应该在a LEFT JOIN b ON b.id = (sub-query)a LEFT JOIN b ON b.id = (sub-query)

SELECT lctn.testID AS assetID, lctn.ClientID, srvc_test.serviceDate, srvc_test.servicePassed
FROM lctn_test AS lctn
LEFT JOIN srvc_test ON srvc_test.srvcTestID = (
    SELECT srvc_test.srvcTestID
    FROM srvc_test
    WHERE srvc_test.testID = lctn.testID
    ORDER BY srvc_test.serviceDate DESC
    LIMIT 1)
WHERE lctn.ClientID = 34
ORDER BY assetID

If you want exactly one row per join you should use a UNIQUE (or PRIMARY) KEY of the joined table in the ON clause. 如果每个联接只需要一行,则应在ON子句中使用联接表的UNIQUE(或PRIMARY)KEY。 That is probably srvc_test.srvcTestID . 那可能是srvc_test.srvcTestID

SELECT lctn.testID AS assetID, lctn.ClientID, srvc_test.serviceDate, srvc_test.servicePassed
FROM lctn_test AS lctn
LEFT JOIN srvc_test ON srvc_test.srvcTestID = (
    SELECT srvc_test.srvcTestID
    FROM srvc_test
    WHERE srvc_test.testID = lctn.testID
    ORDER BY srvc_test.serviceDate DESC
    LIMIT 1)
WHERE lctn.ClientID = 34
ORDER BY assetID

Your query is missing the actual join condition: 您的查询缺少实际的join条件:

SELECT lctn.testID AS assetID, lctn.ClientID, srvc_test.serviceDate, srvc_test.servicePassed
FROM lctn_test lctn LEFT JOIN
     srvc_test st
     ON st.testID = lctn.testID AND
        st.testID = (SELECT st2.testID
                     FROM srvc_test st2
                     WHERE st2.testID = st.testID
                     ORDER BY st.serviceDate DESC
                     LIMIT 1
                    )
WHERE lctn.ClientID = 34
ORDER BY assetID;

I switched the correlation conditions to be based on srvc_test rather than lctn_test . 我将相关条件切换为基于srvc_test而不是lctn_test This doesn't really make a difference. 这并没有真正的改变。 I just find it simpler to be referring to a single table for this purpose. 我只是发现为此目的引用单个表更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM