[英]Join 2 many-to-many tables and get 3 adjacent boolean columns
Let's say there are three tables A
, B
and C
where假设有三个表
A
, B
和C
其中
Table A is:表 A 是:
╔═══════╗
║ col_1 ║
╠═══════╣
║ a ║
║ b ║
║ c ║
╚═══════╝
Table B is:表 B 是:
╔═══════╗
║ col_2 ║
╠═══════╣
║ o ║
║ p ║
║ q ║
╚═══════╝
Table C is:表 C 为:
╔═══════╗
║ c_col ║
╠═══════╣
║ x ║
║ y ║
║ z ║
╚═══════╝
Let's say there is a many-to-many relationship between table A and table C via a mapping table A_to_C
shown below:假设表 A 和表 C 之间通过如下所示的映射表
A_to_C
存在多对多关系:
╔═══════╦═══════╗
║ col_1 ║ c_col ║
╠═══════╬═══════╣
║ a ║ x ║
║ a ║ y ║
║ b ║ y ║
║ b ║ z ║
║ c ║ x ║
╚═══════╩═══════╝
Also there is a many-to-many relationship between table B and table C via a mapping table B_to_C
shown below:表 B 和表 C 之间也存在多对多关系,通过如下所示的映射表
B_to_C
:
╔═══════╦═══════╗
║ col_2 ║ c_col ║
╠═══════╬═══════╣
║ o ║ y ║
║ o ║ z ║
║ p ║ x ║
║ q ║ x ║
║ q ║ y ║
╚═══════╩═══════╝
For value 'b'
in col_1
and value 'q'
in col_2
I need to get the result as:对于
col_1
中的值'b'
和col_2
中的值'q'
,我需要得到结果:
╔═══════╦═══════╦════════╗
║ c_col ║ col_1 ║ col_2 ║
╠═══════╬═══════╬════════╣
║ x ║ NULL ║ q ║
║ y ║ b ║ q ║
║ z ║ b ║ NULL ║
╚═══════╩═══════╩════════╝
which can later be transformed into:稍后可以转换为:
╔═══════╦═══════╦═══════╗
║ c_col ║ col_1 ║ col_2 ║
╠═══════╬═══════╬═══════╣
║ x ║ False ║ True ║
║ y ║ True ║ True ║
║ z ║ True ║ False ║
╚═══════╩═══════╩═══════╝
I asked a similar question here but in this case due to many to many relationships with duplicate column values I am not able to achieve the above result without duplicate columns using the solution in the above link.我在这里问了一个类似的问题,但在这种情况下,由于与重复列值的多对多关系,我无法使用上述链接中的解决方案在没有重复列的情况下实现上述结果。
You can use the following query:您可以使用以下查询:
select c.c_col, ac.col_1 is not null, bc.col_2 is not null
from C c
left join (select * from A_to_C where col_1 = 'b') ac on c.c_col = ac.c_col
left join (select * from B_to_C where col_2 = 'b') bc on c.c_col = bc.c_col
where (ac.col_1, bc.col2) = ('b', 'q')
You don't need the two LEFT JOINS and the IS NOT NULL;您不需要两个 LEFT JOINS 和 IS NOT NULL;
EXISTS()
already yields a boolean: EXISTS()
已经产生了 boolean:
SELECT C.c_col
, EXISTS( SELECT *
FROM A_to_C ac
WHERE ac.c_col = C.c_col AND ac.col_1 = 'b'
) AS col_1
, EXISTS( SELECT *
FROM B_to_C bc
WHERE bc.c_col = C.c_col AND bc.col_2 = 'q'
) AS col_2
FROM C
;
You can generate this result with a series of LEFT JOIN
s, placing your col_1
and col_2
filters in the JOIN
conditions:您可以使用一系列
LEFT JOIN
生成此结果,将col_1
和col_2
过滤器置于JOIN
条件中:
SELECT C.c_col, ac.col_1, bc.col_2
FROM C
LEFT JOIN A_to_C ac ON ac.c_col = C.c_col AND ac.col_1 = 'b'
LEFT JOIN B_to_C bc ON bc.c_col = C.c_col AND bc.col_2 = 'q'
Output: Output:
c_col col_1 col_2
x (null) q
y b q
z b (null)
Demo on SQLFiddle SQLFiddle 上的演示
Those values can then be converted into booleans by testing they are not NULL
:然后可以通过测试这些值不是
NULL
将这些值转换为布尔值:
SELECT C.c_col, ac.col_1 IS NOT NULL AS col_1, bc.col_2 IS NOT NULL AS col_2
FROM C
LEFT JOIN A_to_C ac ON ac.c_col = C.c_col AND ac.col_1 = 'b'
LEFT JOIN B_to_C bc ON bc.c_col = C.c_col AND bc.col_2 = 'q'
Output: Output:
c_col col_1 col_2
x false true
y true true
z true false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.