我在数据库中有两个表。表1包括可以为NULL的订单号。表2包含所有订单数据(包括订单号)。
现在我想选择表1中的所有列和表2中的所有订单数据。因此,如果表1中的某个条目不包含此订单号,则所有其他列应为null。但如果它确实包含订单号,我希望它链接到第二个表并选择这些数据。
输出应该是这样的:
column1tab1 column2tab1 order_number product amount
xx yy 123 p1 2
xx yy 456 p3 4
xx yy NULL NULL NULL
xx yy 789 p2 1
etc...
我尝试了不同的东西,但是我只获得了带有订单号的所有行或者只有null的所有行,但我不能同时获得它们。有人知道解决方案,所以我可以在一个查询中执行此操作吗?
答案 0 :(得分:1)
select *
from orders o left join orderdata od on o.orderId=od.orderid
答案 1 :(得分:0)
select t1.*
from table1 as t1 right join table2 as t2 on t1.order_number=t2.order_number