我有一种情况,我想从客户表中返回空值,并且本质上相当于在账单表中的账单代码上选择不同的值。
我该怎么做?
表客户
ID 姓名 客户编号 帐单代码
表账单
ID 帐单代码
我正在尝试做类似的事情
Select Null ID, Null NAME, Null CUSTNUMBER
from CUSTOMER
RIGHT JOIN ID, BILLCODE
on customer.billcode = bill.billcode
我只想要表中每个账单代码的 1 条记录。
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
I am guessing that the question is: how can I return all customers from the customer table with a list of each bill code they have?
If so, you want to use group by
and 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