【发布时间】:2018-08-07 15:49:43
【问题描述】:
我创建了以下查询,其中包括 2 个连接并且工作正常:
SELECT a.name, b.tid
FROM table1 a INNER JOIN
table2 b
ON a.id = b.id
INNER JOIN (
SELECT b2.id, b2.status, MAX(b2.created_time) as max_time
FROM oac.qualys_scan b2
GROUP BY b2.id , b2.qualys_type
) t on t.id = a.id
AND t.status=b.status
AND t.max_time = b.created_time
WHERE b.status = 'FAIL';
输出如下:
id tid name
17695 19512 abc.com
17781 19628 abc1.com
17805 19732 abc2.com
17806 19703 abc3.com
17807 19704 abc4.com
我有另一个表 table3,它具有以下值
id tid name details
842 19512 abc.com Details Description 1
843 19628 abc1.com Details Description 2
我想将上述查询与tid 上的 table3 连接起来,以便得到以下输出
id tid name details
17695 19512 abc.com Details Description 1
17781 19628 abc1.com Details Description 1
我正在使用以下查询,但它只返回不正确的一行
SELECT a.name, b.tid
FROM table1 a INNER JOIN
table2 b
ON a.id = b.id
INNER JOIN (
SELECT b2.id, b2.status, MAX(b2.created_time) as max_time
FROM oac.qualys_scan b2
GROUP BY b2.id , b2.qualys_type
) t on t.id = a.id
AND t.status=b.status
AND t.max_time = b.created_time
INNER JOIN table3 T3 on b.tid = T3.tid
WHERE b.status = 'FAIL'
【问题讨论】:
-
如果将
inner join替换为left join table3 T3,返回多少行? -
感谢输入和输出表。但是,请阅读minimal reproducible example 并采取行动,以便可以剪切、粘贴和运行所有必要的代码——包括 DDL 和输入和输出。
标签: mysql select join inner-join outer-join