MySQL与计数和分组方式的完全连接
我有一张这样的桌子:MySQL与计数和分组方式的完全连接,mysql,Mysql,我有一张这样的桌子: | id | col1 | col2 | col3 | valid | | 1 | apple | | | 1 | | 2 | lemon | apple | | 1 | | 3 | lemon | pear | | 1 | | 4 | carrot | apple | | 1 | | 5 | potatoe
| id | col1 | col2 | col3 | valid |
| 1 | apple | | | 1 |
| 2 | lemon | apple | | 1 |
| 3 | lemon | pear | | 1 |
| 4 | carrot | apple | | 1 |
| 5 | potatoes | tomatoes | apple | 1 |
| 6 | banana | | | 0 |
| 6 | banana | | | 1 |
我需要创建一个MySQL查询,它返回每列中每个条目的计数。理想的情况是:
| apple | 1 | 2 | 1
| carrot | 1 | null | null
| lemon | 2 | null | null
| pear | null | 1 | null
| potatoes | 1 | null | null
| tomatoes | null | 1 | null
| banana | 1 | null | null
格式可能不同,我的结果应该显示某个值在某个列中出现的次数
只能用一个查询完成,我不能使用任何其他语言、任何其他子结果等,只是:
(one query)
`input` -> `output`.
我的最佳尝试是:
SELECT *
FROM (SELECT col1, COUNT(col1) FROM sometable WHERE valid = 1 GROUP BY col1) t1
LEFT JOIN (SELECT col2, COUNT(col2) FROM sometable WHERE valid = 1 GROUP BY col2) t2
ON t1.col1 = t2.col2
LEFT JOIN (SELECT col3, COUNT(col3) FROM sometable WHERE valid = 1 GROUP BY col3) t3
ON t1.col1 = t3.col3
但是使用LEFT JOIN
时,我丢失了一些行(值仅在第二列或第三列),我尝试了几种不同的“JOIN-s”,在MySQL中取得了成功
编辑:这些值是动态的,我不知道确切的唯一可能性,在这之前您是正确的,只需要再多一个子查询,您可以使用
union distinct
列出3列中所有可能的值,并且您需要子查询分别获得每个列的计数:
select f.fruits, m1.fruit_count1, m2.fruit_count2, m3.fruit_count3
from
(select distinct col1 as fruits from mytable
union
select distinct col2 from mytable
union
select distinct col3 from mytable) f
left join
(select col1, count(col1) as fruit_count1 from mytable where valid=1 group by col1) m1 on f.fruits=m1.col1
left join
(select col2, count(col2) as fruit_count2 from mytable where valid=1 group by col2) m2 on f.fruits=m2.col2
left join
(select col3, count(col3) as fruit_count3 from mytable where valid=1 group by col3) m3 on f.fruits=m3.col3;
您所处的轨道是正确的,只需再增加一个子查询,其中使用
union distinct
列出3列中所有可能的值,并且需要子查询分别获得每个列的计数:
select f.fruits, m1.fruit_count1, m2.fruit_count2, m3.fruit_count3
from
(select distinct col1 as fruits from mytable
union
select distinct col2 from mytable
union
select distinct col3 from mytable) f
left join
(select col1, count(col1) as fruit_count1 from mytable where valid=1 group by col1) m1 on f.fruits=m1.col1
left join
(select col2, count(col2) as fruit_count2 from mytable where valid=1 group by col2) m2 on f.fruits=m2.col2
left join
(select col3, count(col3) as fruit_count3 from mytable where valid=1 group by col3) m3 on f.fruits=m3.col3;
您需要项目分别来自哪个列的大小写和标识符的总和
更有效的方法是预先聚合每列的计数,特别是在处理大型数据集时
select
item,
sum( case when whichCol = 1 then TotRecs else 0 end ) as FoundCol1,
sum( case when whichCol = 2 then TotRecs else 0 end ) as FoundCol2,
sum( case when whichCol = 3 then TotRecs else 0 end ) as FoundCol3
from
( select col1 as item, max(1) as whichCol, count(*) as TotRecs
from YourTable
group by col1
union all
select col2 as item, max(2) as whichCol, count(*) as TotRecs
from YourTable
group by col2
union all
select col3 as item, max(3) as whichCol, count(*) as TotRecs
from YourTable
group by col3 ) allRows
group by
item
您需要项目分别来自哪个列的大小写和标识符的总和
更有效的方法是预先聚合每列的计数,特别是在处理大型数据集时
select
item,
sum( case when whichCol = 1 then TotRecs else 0 end ) as FoundCol1,
sum( case when whichCol = 2 then TotRecs else 0 end ) as FoundCol2,
sum( case when whichCol = 3 then TotRecs else 0 end ) as FoundCol3
from
( select col1 as item, max(1) as whichCol, count(*) as TotRecs
from YourTable
group by col1
union all
select col2 as item, max(2) as whichCol, count(*) as TotRecs
from YourTable
group by col2
union all
select col3 as item, max(3) as whichCol, count(*) as TotRecs
from YourTable
group by col3 ) allRows
group by
item
您正在寻找
pivoting
您正在寻找pivoting
谢谢您的想法,但它不起作用,我从问题中得到的样本值apple 2
谢谢您的反馈,但是,由于它基本上说我的解决方案在没有提供任何提示的情况下无法工作。我使用的方法与您编写的完全相同,只是我将mytable
更改为sometable
,并在最后一行添加s
,因此它是fruits
检查更新的答案。修改代码以在单独的子查询中获得计数。我的反应只是稍微慢一点,你的代码太棒了!你真的帮助了我,非常感谢Hanks的想法,但它不起作用,我从我的问题中得到的样本值apple 2
感谢你的反馈,但因为它基本上说我的解决方案不起作用,如果不提供任何提示,说明你到底是如何尝试实现queryI的,请使用与你所写的完全相同的方法,只有我将mytable
更改为sometable
,并在最后一行添加s
,因此它是fruits
查看更新的答案。修改代码以在单独的子查询中获得计数。我的反应只是稍微慢一点,你的代码太棒了!你真的帮了我,非常感谢