简体   繁体   English

MySQL加入Subquery

[英]MySQL Join on Subquery

I am trying to make a join on a subquery although I can't get the results I require. 我试图在子查询上进行连接,尽管我无法得到我需要的结果。 Can someone either suggest a better query to run or perhaps I shouldn't use a subquery (I am not well versed in any variant of SQL), is there a better query type to run ? 有人可以建议更好的查询运行或者我不应该使用子查询(我不熟悉SQL的任何变体),是否有更好的查询类型运行?

I can see with my limited SQL knowledge that the subquery isn't in the best place. 我可以通过我有限的SQL知识看到子查询不在最佳位置。 The goal here is to query for fields from tblResults and also join them to the last row in tblTraceOutput that has the same resultid (there are multiple rows in tblTraceOutput that have the same resultid value, so just the last row). 这里的目标是从tblResults查询字段,并将它们连接到具有相同resultid tblTraceOutput中的最后一行(tblTraceOutput中有多个行具有相同的resultid值,因此只是最后一行)。

SELECT r.`resultid`, r.`successful`, r.`result`, r.`testdate`, r.`changed`, 
t.`delay1`, t.`delay2`, t.`delay3` 
FROM `tblResults` AS r 
JOIN (
    SELECT `resultid`, `delay1`, `delay2`, `delay3` 
    FROM `tblTraceOutput` 
    WHERE `traceid`='48'
    ORDER BY `outputid` DESC LIMIT 0,1
) AS t ON (t.`resultid` = r.`resultid`) 
WHERE r.`traceid` = '48' ORDER BY r.`resultid` DESC LIMIT 0,20

If I change that JOIN to a LEFT JOIN I will get more results back from my query but with 'NULL' written in the three columns for t.delay1 , t.delay2 and t.delay3 , in every row except the first . 如果我将JOIN更改为LEFT JOIN我将从查询中获得更多结果,但在t.delay1t.delay2t.delay3的三列中写入“NULL”, 除了第一行之外的每一行 The results pulled from the subquery are only joint to the first row of output from the main query on tblResults. 从子查询中提取的结果仅与tblResults上的主查询的第一行输出相关联。 How can I have this subquery run and joint for every row of output in the outer query? 如何在外部查询中为每行输出运行此子查询?

In my head I imagine the following, but I can't make it work in any way: 在我脑海中,我想到了以下几点,但我不能以任何方式使它工作:

SELECT r.`resultid`, r.`successful`, r.`result`, r.`testdate`, r.`changed` 
FROM `tblResults` AS r 
(
    SELECT t.`resultid`, t.`delay1`, t.`delay2`, t.`delay3` 
    FROM `tblTraceOutput` 
    WHERE `traceid`='48'
    ORDER BY `outputid` DESC LIMIT 0,1
) AS t
JOIN ON (t.`resultid` = r.`resultid`) 
WHERE r.`traceid` = '48' ORDER BY r.`resultid` DESC LIMIT 0,20
SELECT r.`resultid`, r.`successful`, r.`result`, r.`testdate`, r.`changed` 
FROM `tblResults` AS r 
LEFT JOIN `tblTraceOutput` as t ON (t.`resultid` = r.`resultid`) 
WHERE r.`traceid` = '48' ORDER BY r.`resultid` DESC LIMIT 0,20

left join will return results from tblTraceOutput table even if there are none that match (the returned data is NULL) left join将返回tblTraceOutput表的结果,即使没有匹配(返回的数据为NULL)

inner join will only return results from resultid and tblTraceOutput that match the ON clause 内连接只返回与ON子句匹配的resultid和tblTraceOutput的结果

I think this is the one you are looking. 我认为这是你正在寻找的那个。 The query uses subquery to separately gets the latest outputid for every resultid and resultid on table tblTraceOutput . 该查询使用子查询分别得到最新outputid为每resultidresultid上表tblTraceOutput The result of the subquery is then joined back with table tblTraceOutput ( itself ) provided that it matches on all column on the subquery. 然后,子查询的结果与表tblTraceOutput本身 )连接在一起,前提是它与子查询上的所有列匹配。 The possible record that will match is the latest one. 可能匹配的记录是最新记录。

SELECT  a.*, b.*
FROM    tblResults a
        INNER JOIN tblTraceOutput b
            ON a.resultid = b.resultid
        INNER JOIN
        (
            SELECT  resultid, traceid, MAX(outputid) max_ID
            FROM    tblTraceOutput
            GROUP   BY resultid, traceid
        ) c ON  b.resultid = c.resultid AND
                b.traceid = c.traceid
                b.outputid = c.max_ID
WHERE   a.traceid = 48
ORDER   BY resultid
LIMIT   0, 20

You can use a LEFT JOIN , just include an expression in the WHERE clause to avoid displaying records where there is no associated record in the joined table: 您可以使用LEFT JOIN ,只需在WHERE子句中包含一个表达式,以避免显示连接表中没有关联记录的记录:

SELECT r.`resultid`, r.`successful`, r.`result`, r.`testdate`, r.`changed`, 
t.`delay1`, t.`delay2`, t.`delay3` 
FROM `tblResults` AS r
LEFT JOIN `tblTraceOutput` AS t ON (t.`resultid` = r.`resultid`)
WHERE r.`traceid` = '48' AND t.`resultid` IS NOT NULL
ORDER BY r.`resultid` DESC
LIMIT 0, 20;

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

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