我正在尝试提出一个查询,它允许我计算同一个表中两列的值。例如
table: rider_geo_addresses
==================================
|id | origin_id | destination_id |
==================================
1 4 8
1 2 6
1 8 4
1 6 2
1 1 5
1 2 8
我想要一个查询,它将给我的起源\标识和目的地\标识组合排序描述计数
table: result
============================
| geo_addresses | counter |
============================
8 3
2 3
6 2
4 2
5 1
1 1
我尝试在两个单独的表上获取值,并使用union将它们组合起来,但它没有考虑可能出现在两列上的相同值。这就是我试过的
select ( origin_id) as geo_addresses, count(origin_id) as counter
from rider_geo_addresses
where rider_profile_id
group by origin_id
union
select ( destination_id) as geo_addresses, count(destination_id) as counter
from rider_geo_addresses
where rider_profile_id
group by destination_id
order by counter desc;
3条答案
按热度按时间ycl3bljg1#
据我所知,你的解决办法是在工会结束后分组
igsr9ssn2#
使用union all而不是union应该可以得到正确的输出。
hwamh0ep3#
你可以用
full join
而不是union all
: