假设我有两张如下表:
表:产品
-------------
|PRODUCT |
-------------
|IPAD |
-------------
|IPHONE |
-------------
表:销售
-----------------------------
SALES_PERSON|PRODUCT|IS_SOLD|
-----------------------------
ALICE |IPAD | 1 |
-----------------------------
JOHN |IPONE | 1 |
-----------------------------
是否可以使SQL语句产生如下结果?
--------------------------------
SALES_PERSON| PRODUCT | IS_SOLD|
--------------------------------
ALICE | IPAD | 1 |
--------------------------------
ALICE | IPHONE | null |
--------------------------------
JOHN | IPAD | null |
--------------------------------
JOHN | IPHONE | 1 |
--------------------------------
答案 0 :(得分:2)
您可以使用连接执行此操作,但您需要多个连接。您需要在产品和销售人员之间创建交叉产品。左边将其连接到sales_person
表:
select pe.sales_person, p.product, sp.is_sold
from product p cross join
(select distinct sales_person from sales) pe left outer join
sales_person sp
on sp.product = p.product and
sp.sales_person = pe.sales_person;