union与union all区别
union all 不删除重复列
A | B |
1 | 2 |
1 | 3 |
2 | 3 |
3 | 4 |
- 组成一张临时表
select A from(select A from table
union all
select B from table)as target_table
A |
1 |
1 |
2 |
3 |
2 |
3 |
3 |
4 |
- 列出A中各组的计数值
select A,count(A)
from (select A from table
union all
select B from table)as target_table
group by A
A | COUNT(A) |
1 | 2 |
2 | 2 |
3 | 3 |
4 | 1 |
- 查询count(a)最大值
select A,count(A)
from(select A,count(A)
from (select A from table
union all
select B from table)as target_table
group by A)
order by count(A) desc
limit 1
A | COUNT(A) |
3 | 3 |