每个唯一ID的SQL连接

时间:2020-11-08 00:51:04

标签: sql sql-server postgresql

enter image description here

我正在尝试在产品ID上同时加入两个表,但是我无法产生预期的结果。我尝试对产品ID进行左联接,但没有产生空值或没有填写每个国家/地区缺少的产品ID。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

使用cross join生成行,然后使用left join

select c.country, t2.product_id, t1.cost
from (select distinct country from table1) c cross join
     table2 t2 left join
     table1 t1
     on t1.country = c.country and t1.product_id = t2.product_id
order by c.country, t2.product_id;

答案 1 :(得分:1)

尝试此操作以获取所需的输出:

mid
相关问题