有两个表Customer和费用(PK customer.CustomerID -> Fees.CustomerId FK)
Customer
表中有客户。
表Fees
中没有当前月份的数据。
没有在当月的费用表中插入数据
我想在客户表中显示客户详细信息,但尚未支付当月费用。
类似的东西:
if(Convert(varchar(3),MonthFee, 109) != convert(varchar(3),getdate(),109))
BEGIN
select Customer.CustomerId as ID, Customer.CustomerName as Name, Customer.Phone as [Phone No], 'UnPaid' as [Pay Status], convert(varchar(3),getdate(),109) as [Month], YEAR(GETDATE()) as [Year]
from Customer inner join Fees on Customer.CustomerId = Fees.CustomerId Where FeeMonth = null
END
感谢帮助,谢谢。
答案 0 :(得分:1)
使用以下查询..
select Customer.CustomerId as ID,
Customer.CustomerName as Name,
Customer.Phone as [Phone No],
'UnPaid' as [Pay Status],
convert(varchar(3),getdate(),109) as [Month],
YEAR(GETDATE()) as [Year]
from Customer
Where not exists(select 1 from Fees
Where Customer.CustomerId = Fees.CustomerId
And Month( FeeMonth )= month(getdate())
And year(FeeMonth)=year(getdate())
答案 1 :(得分:0)
您需要使用其他类型的联接。最简单的方法是使用右连接,如下所示
select Customer.CustomerId as ID, Customer.CustomerName as Name, Customer.Phone as [Phone No], 'UnPaid' as [Pay Status], convert(varchar(3),getdate(),109) as [Month], YEAR(GETDATE()) as [Year]
from Fees
right join Customer on Fees.CustomerId = Customer.CustomerId Where FeeMonth = null