问题描述
我在数据库中有两个表.表1包含一个可能为NULL的订单号.表2包含所有订单数据(包括订单号).
I have two tables in a database. Table 1 includes an order number which could be NULL. Table two contains all order data (including the order number).
现在,我想从表1中选择所有列,并从表2中选择所有订单数据.因此,如果表一中的某个条目不包含此订单号,则所有其他列均应为null.但是,如果确实包含订单号,我希望将其链接到第二张表并选择这些数据.
Now I want to select all columns from table 1 and all order data from table two. So if a certain entry in table one doesn't contains this order number, all other columns should be null. But if it does contain an order number I want it to be linked to the second table and have these data selected.
输出应类似于:
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...
我尝试了不同的方法,但是我只获得了所有带有订单号的行或全部为空的行,但是我无法同时获得它们.有人知道解决方案吗,所以我可以在一个查询中做到这一点?
I tried different things, but I only get all the rows with an order number or all with null, but I can't get them both at the same time. Does someone know a solution, so I can do this in one query?
推荐答案
您尝试了什么?一个简单的左联接就可以解决问题.示例:
what did you try? A simple left join would do the trick.Example:
select *
from orders o left join orderdata od on o.orderId=od.orderid
这篇关于从两个表中选择,其中链接列可以为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!