首页 > 解决方案 > 有没有办法在 Postgresql 中获得以下结果

问题描述

所以有2张桌子。

表格1

   month | orders
    Jan  |  10
    Feb  |  4
    Apr  |  7

表2

   month | dispatch
    Jan  | 2
    May  | 3

我想得到以下结果:-

   month | orders | dispatch
    Jan  |   10   |   2
    Feb  |   4    |   0
    Apr  |   7    |   0
    May  |   0    |   3

有什么方法可以解决这个问题?

标签: sqlpostgresql

解决方案


使用完全外连接

  select coalesce(t1.month,t2.month),coalesce(t1.orders,0)
 ,coalesce(t2.dispatch,0)
  from table1 t1 full outer join table2 t2 on t1.month=t2.month

推荐阅读