如何在左连接分支内强制连接?

发布于 2024-10-03 01:42:29 字数 497 浏览 4 评论 0原文

我需要 LEFT 连接到整个 t2+t3 分支,但是如果我可以找到 t1 和 t2 之间的匹配连接,我想强制执行 t2 和 t3 连接。

   SELECT T1.name,T2.bob,T3.a
     FROM T1
LEFT JOIN T2 ON t1.id = t2.t1_id
     JOIN T3 ON t2.id = T3.t2_id

语法是什么?

样本数据:

T1 [id,name]
1 aaa
2 bbb
3 ccc

T2 [id,t1_id,bob]
1,1,777
2,1,888
2,2,999

T3[id,t2_id,a]
1,2,'yeh'

预期结果:

 [name] , [a]   , [bob]
  aaa   , 'yeh' , 888
  bbb   , NULL  , NULL
  ccc   , NULL  , NULL

I need to LEFT join to entire t2+t3 branch, but if I can find a matching join between t1 and t2, I want to enforce the t2 and t3 join.

   SELECT T1.name,T2.bob,T3.a
     FROM T1
LEFT JOIN T2 ON t1.id = t2.t1_id
     JOIN T3 ON t2.id = T3.t2_id

What is the syntax?

Sample data:

T1 [id,name]
1 aaa
2 bbb
3 ccc

T2 [id,t1_id,bob]
1,1,777
2,1,888
2,2,999

T3[id,t2_id,a]
1,2,'yeh'

EXPECTED RESULT:

 [name] , [a]   , [bob]
  aaa   , 'yeh' , 888
  bbb   , NULL  , NULL
  ccc   , NULL  , NULL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缱倦旧时光 2024-10-10 01:42:29

编辑:此查询返回示例数据中预期的结果 -
(另请注意,此查询是在下面 Treefrog 的答案的帮助下进行的) -

SELECT t1.[name], t3.a, t2.bob
FROM T2 as t2
JOIN T3 as t3 ON t3.t2_id = t2.id
RIGHT JOIN T1 as t1 ON t1.id = t2.t1_id

我的旧答案 -

SELECT a
FROM T1 as t1
INNER JOIN T2 as t2 ON t1.id = t2.t1_id
LEFT JOIN T3 as t3 ON t2.id = t3.t2_id

EDIT: This query returns you the result as expected from your sample data -
(Also note, this query is taken with help from Treefrog's answer below) -

SELECT t1.[name], t3.a, t2.bob
FROM T2 as t2
JOIN T3 as t3 ON t3.t2_id = t2.id
RIGHT JOIN T1 as t1 ON t1.id = t2.t1_id

My Older answer -

SELECT a
FROM T1 as t1
INNER JOIN T2 as t2 ON t1.id = t2.t1_id
LEFT JOIN T3 as t3 ON t2.id = t3.t2_id
笙痞 2024-10-10 01:42:29
SELECT T1.a
FROM T2
JOIN T3 ON T3.t2_id = T2.id
RIGHT JOIN T1 ON T1.id = T2.t1_id
SELECT T1.a
FROM T2
JOIN T3 ON T3.t2_id = T2.id
RIGHT JOIN T1 ON T1.id = T2.t1_id
孤独患者 2024-10-10 01:42:29

这两个都会给你结果。不确定哪个会表现更好。

嵌套语句:

SELECT [name], NULL AS [a], NULL AS  [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NULL
AND (SELECT COUNT(*) FROM t1 AS t1b LEFT OUTER JOIN t2 AS t2b ON t1b.id = t2b.t1_id LEFT OUTER JOIN t3 AS t3b ON t2b.id = t3b.t2_id WHERE t1.id = t1b.id AND t3b.a IS NOT NULL) = 0
UNION
SELECT [name], [a], [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NOT NULL
ORDER BY t1.name

临时表:

CREATE TABLE #tmp_Rslt([name] varchar(50), [a] varchar(50), [bob] varchar(50))

--select matches
INSERT INTO #tmp_Rslt
SELECT [name], [a], [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NOT NULL
ORDER BY t1.name

--select t1's that didn't have matches
INSERT INTO #tmp_Rslt
SELECT [name], NULL AS [a], NULL AS  [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NULL
AND t1.[name] NOT IN (SELECT DISTINCT [name] FROM #tmp_Rslt)

SELECT *
FROM #tmp_Rslt

--cleanup.
DROP TABLE #tmp_Rslt

Both of these will give you your result. Not sure which would perform better.

NESTED STATEMENT:

SELECT [name], NULL AS [a], NULL AS  [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NULL
AND (SELECT COUNT(*) FROM t1 AS t1b LEFT OUTER JOIN t2 AS t2b ON t1b.id = t2b.t1_id LEFT OUTER JOIN t3 AS t3b ON t2b.id = t3b.t2_id WHERE t1.id = t1b.id AND t3b.a IS NOT NULL) = 0
UNION
SELECT [name], [a], [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NOT NULL
ORDER BY t1.name

TEMPORARY TABLE:

CREATE TABLE #tmp_Rslt([name] varchar(50), [a] varchar(50), [bob] varchar(50))

--select matches
INSERT INTO #tmp_Rslt
SELECT [name], [a], [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NOT NULL
ORDER BY t1.name

--select t1's that didn't have matches
INSERT INTO #tmp_Rslt
SELECT [name], NULL AS [a], NULL AS  [bob]
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.t1_id
LEFT OUTER JOIN t3 ON t2.id = t3.t2_id 
WHERE t3.t2_id IS NULL
AND t1.[name] NOT IN (SELECT DISTINCT [name] FROM #tmp_Rslt)

SELECT *
FROM #tmp_Rslt

--cleanup.
DROP TABLE #tmp_Rslt
冷默言语 2024-10-10 01:42:29

这应该适用于 MySQL

SELECT * FROM T1
LEFT JOIN (T2
 INNER JOIN T3 ON T2.id=T3.t2_id
) ON T1.id= T2.t1_id

This should work in MySQL

SELECT * FROM T1
LEFT JOIN (T2
 INNER JOIN T3 ON T2.id=T3.t2_id
) ON T1.id= T2.t1_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文