[英]Why do left and right join ignore some values in a query?
I have a table with 3 columns and these values:我有一个包含 3 列的表和这些值:
col1 col2 col3
-------------------
1 2 8
1 3 5
1 10 15
2 4 6
2 9 7
3 5 6
I join a query LEFT JOIN and RIGHT JOIN a grouping and counting query for each number (MS-ACCESS).我加入查询 LEFT JOIN 和 RIGHT JOIN 对每个数字 (MS-ACCESS) 的分组和计数查询。
SELECT Col1 AS Num, t1.CON1, t2.CON2, t3.CON3
FROM
(((SELECT col1, COUNT(col1) AS CON1 FROM table GROUP BY col1) AS t1
LEFT JOIN (SELECT col2, COUNT(col2) AS CON2 FROM table GROUP BY col2) AS t2
ON t1.col1 = t2.col2)
LEFT JOIN (SELECT col3, COUNT(col3) AS CON3 FROM table GROUP BY col3) AS t3
ON t2.col2 = t3.col3)
UNION
SELECT col3 AS Num, t1.CON1, t2.CON2, t3.CON3
FROM
(((SELECT col1, COUNT(col1) AS CON1 FROM table GROUP BY col1) AS t1
RIGHT JOIN (SELECT col2, COUNT(col2) AS CON2 FROM table GROUP BY col2) AS t2
ON t1.col1 = t2.col2)
RIGHT JOIN (SELECT col3, COUNT(col3) AS CON3 FROM table GROUP BY col3) AS t3
ON t2.col1 = t3.col3)
It results like this:结果如下:
Num CON1 CON2 CON3
--------------------------
1 3
2 2 1
3 1 1
5 1 1
6 2
7 1
8 1
15 1
But this query ignores count of values from column 2 of table但是此查询忽略了表第 2 列中的值计数
Num CON2
---------------
4 1
9 1
10 1
What is missing in my query?我的查询中缺少什么?
If you want to count each value and the number of times in each column, then use union all
to split the data and then group by
:如果要计算每列中的每个值和次数,请使用
union all
拆分数据,然后group by
:
select num, sum(col1), sum(col2), sum(col3)
from ((select col1 as num, 1 as col1, 0 as col2, 0 as col3
from t
) union all
(select col2 as num, 0 as col1, 1 as col2, 0 as col3
from t
) union all
(select col3 as num, 0 as col1, 0 as col2, 1 as col3
from t
)
) as x
group by num
order by num;
This gives correct totals, but I can't figure how to summarise the results (it lists 3 twice, for example)...这给出了正确的总数,但我不知道如何总结结果(例如,它列出了 3 次)......
(
(SELECT col1 as num, COUNT(col1) AS CON1, null as CON2, null as CON3 FROM mytable t1
LEFT JOIN (SELECT col2, COUNT(col2) AS CON2 FROM mytable GROUP BY col2) t2
ON t1.col1 = t2.col2
LEFT JOIN (SELECT col3, COUNT(col3) AS CON3 FROM mytable GROUP BY col3) t3
ON t2.col2 = t3.col3
GROUP BY t1.col1)
UNION
(SELECT col2 as num, null as CON1, COUNT(col2) AS CON2, null as CON3 FROM mytable t4
left JOIN (SELECT col1, COUNT(col1) AS CON1 FROM mytable GROUP BY col1) t5
ON t4.col2 = t5.col1
left JOIN (SELECT col3, COUNT(col3) AS CON3 FROM mytable GROUP BY col3) t6
ON t4.col2 = t6.col3
GROUP BY t4.col2)
UNION
(SELECT col3 as num, null as CON1, null as CON2, COUNT(col3) AS CON3 FROM mytable t7
left JOIN (SELECT col1, COUNT(col1) AS CON1 FROM mytable GROUP BY col1) t8
ON t7.col3 = t8.col1
left JOIN (SELECT col2, COUNT(col2) AS CON3 FROM mytable GROUP BY col2) t9
ON t7.col3 = t9.col2
GROUP BY t7.col3)
)
RESULTS:结果:
num CON1 CON2 CON3
1 3
2 1
2 2
3 1
3 1
4 1
5 1
5 1
6 2
7 1
8 1
9 1
10 1
15 1
... Anyone? ... 任何人?
SQL TEST: https://sqltest.net/#979886 SQL 测试: https://sqltest.net/#979886
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.