我有一种情况,我希望从customer表中返回null值,并且相当于bill表中Bill代码的select distinct。
我该怎么做?
表客户
ID Name CustNumber BILLCODE
表格
ID BILLCODE
我正在尝试做类似
的事情Select Null ID, Null NAME, Null CUSTNUMBER
from CUSTOMER
RIGHT JOIN ID, BILLCODE
on customer.billcode = bill.billcode
我只希望表格中的每个账单代码都有1条记录。
答案 0 :(得分:0)
with billcodes as (select distinct billcode from bill)
select b.billcode, c.id, c.name, c.custnumber
from billcodes b
left outer join customer c on c.billcode=b.billcode
或者:
select b.billcode, c.id, c.name, c.custnumber
from (select distinct billcode from bill) b
left outer join customer c on c.billcode=b.billcode
答案 1 :(得分:0)
我猜这个问题是:如何从客户表中返回所有客户的每个账单代码清单?
如果是,您要使用group by
和left outer join
:
select b.billcode, c.id, c.name, c.custnumber
from customer c left outer join
bill b
on c.billcode = b.billcode
group by b.billcode, c.id, c.name, c.custnumber