如何在SQL Union Statemet中查找NULL值

时间:2019-05-01 07:55:17

标签: sql database ms-access union

无法找到尚未从SQL语句进行的付款 需要查找并列出所有未付款的发票

我在运行此SQL时遇到“输入参数”,尽管没有设置任何条件

请在下面找到代码

我已经厌倦了使用=并且是

SELECT TraderAccomodationPaymentNo as PaymentNo, AmountPaid as PaidAmount, DateOfPayment as DatePaid, TraderAccomodationInvoiceNo as InvoiceNo, PaymentMethod as PaymentType
FROM [TraderAccomodationPayment] 
WHERE PaidAmount IS NULL

UNION 

SELECT TraderSeminarPaymentNo as PaymentNo, AmountPaid as PaidAmount, DateOfPayment as DatePaid, TraderSeminarInvoiceNo as InvoiceNo, PaymentMethod as PaymentType
FROM [TraderSeminarPayment]
WHERE PaidAmount IS NULL

UNION

SELECT AttendeeAccommodationPaymentNo as PaymentNo, AmountPaid as PaidAmount, DateOfPayment as DatePaid, AttendeeAccommodationInvoiceNo as InvoiceNo, PaymentMethod as PaymentType
FROM [AttendeeAccomodationPayment]
WHERE PaidAmount IS NULL

UNION 

SELECT AttendeeSeminarPaymentNo as PaymentNo, AmountPaid as PaidAmount, DateOfPayment as DatePaid, AttendeeSeminarInvoiceNo as InvoiceNo, PaymentMethod as PaymentType
FROM [AttendeeSeminarPayment]
WHERE PaidAmount IS NULL

我希望能够找到所有未付的发票。

1 个答案:

答案 0 :(得分:0)

您不能在WHERE中使用列别名。 MS Access无法识别PaidAmount,因此假定它是一个参数并要求您输入值。

您需要使用原始列名。我也强烈建议UNION ALL,除非您要承担删除重复项的额外开销。

SELECT TraderAccomodationPaymentNo as PaymentNo, 
       AmountPaid as PaidAmount,
       DateOfPayment as DatePaid, 
       TraderAccomodationInvoiceNo as InvoiceNo, 
       PaymentMethod as PaymentType
FROM TraderAccomodationPayment
WHERE AmountPaid IS NULL
UNION ALL
SELECT TraderSeminarPaymentNo as PaymentNo,
       AmountPaid as PaidAmount,
       DateOfPayment as DatePaid, 
       TraderSeminarInvoiceNo as InvoiceNo,
       PaymentMethod as PaymentType
FROM TraderSeminarPayment
WHERE AmountPaid IS NULL
UNION ALL
SELECT AttendeeAccommodationPaymentNo as PaymentNo,
       AmountPaid as PaidAmount,
       DateOfPayment as DatePaid, 
       AttendeeAccommodationInvoiceNo as InvoiceNo,
       PaymentMethod as PaymentType
FROM AttendeeAccomodationPayment
WHERE AmountPaid IS NULL
UNION ALL
SELECT AttendeeSeminarPaymentNo as PaymentNo, 
       AmountPaid as PaidAmount,
       DateOfPayment as DatePaid, 
       AttendeeSeminarInvoiceNo as InvoiceNo, 
       PaymentMethod as PaymentType
FROM AttendeeSeminarPayment
WHERE AmountPaid IS NULL;

方括号也多余。