从SQL查询和错误无效的列名返回特定数据

时间:2017-05-24 13:15:56

标签: sql sql-server

下面的SQL查询返回了我需要的数据,但是在表格列"付款状态","等待付款"并且"付费"为组织退还,我希望退回给公司的付款状态(付费/未付),而不是上述。

我当时认为这可能是一个where子句WHERE付款状态仅等于"付费" AND" Unpaid",我试图这样做,我有一个错误

  

无效的列名称Payment_Status

请告知我如何才能显示付款状态(付费/未付款)

SELECT        
    Account.mm_registrationnumber AS CL_Reg_Number, 
    Account.Name, 
    Account.mm_supplierstatus AS Supplier_Status, 
    Account.PrimaryContactIdName AS Primary_Contact, 
    Account.EMailAddress1 AS Email, 
    Account.Telephone1 AS Telephone, 
    mm_address.mm_line1 AS Line1, 
    mm_address.mm_line2 AS Line2, 
    mm_address.mm_line3 AS Line3, 
    mm_address.mm_line4 AS Line4, 
    mm_address.mm_city AS City, 
    mm_address.mm_county AS County, 
    mm_address.mm_postcode AS Postcode, 
    mm_turnover.mm_name AS Signup_Turnover, 
    Account.mm_signupdate AS SignUp_Date, 
    mm_payment.mm_paymenttype AS Payment_Type, 
    mm_payment.mm_paymentstatus AS Payment_Status, 
    mm_payment.mm_vatamount AS Payment_Amount_VAT, 
    mm_payment.mm_paymentamount AS Payment_Amount_Net, 
    Invoice.InvoiceNumber AS Invoice_ID, 
    Invoice.DueDate AS Due_Date, 
    Invoice.CreatedOn AS Created_Date
FROM  
    Account 
INNER JOIN 
    mm_payment ON mm_payment.mm_organisation = Account.AccountId 
INNER JOIN 
    mm_address ON Account.mm_address = mm_address.mm_addressId 
INNER JOIN 
    mm_turnover ON Account.mm_turnover = mm_turnover.mm_turnoverId 
INNER JOIN 
    Invoice ON Invoice.AccountId = Account.AccountId
WHERE 
    Payment_Status = 'Paid'; //  error 'invalid column name Payment_Status'

2 个答案:

答案 0 :(得分:3)

您的问题是您在WHERE子句中引用了列别名,而不是列本身。试试这个:

SELECT        Account.mm_registrationnumber AS CL_Reg_Number, Account.Name, Account.mm_supplierstatus AS Supplier_Status, 
                     Account.PrimaryContactIdName AS Primary_Contact, Account.EMailAddress1 AS Email, Account.Telephone1 AS Telephone, mm_address.mm_line1 AS Line1, 
                     mm_address.mm_line2 AS Line2, mm_address.mm_line3 AS Line3, mm_address.mm_line4 AS Line4, mm_address.mm_city AS City, 
                     mm_address.mm_county AS County, mm_address.mm_postcode AS Postcode, mm_turnover.mm_name AS Signup_Turnover, Account.mm_signupdate AS SignUp_Date, 
                     mm_payment.mm_paymenttype AS Payment_Type, mm_payment.mm_paymentstatus AS Payment_Status, mm_payment.mm_vatamount AS Payment_Amount_VAT, 
                     mm_payment.mm_paymentamount AS Payment_Amount_Net, Invoice.InvoiceNumber AS Invoice_ID, Invoice.DueDate AS Due_Date, 
                     Invoice.CreatedOn AS Created_Date

FROM            Account INNER JOIN
                     mm_payment ON mm_payment.mm_organisation = Account.AccountId INNER JOIN
                     mm_address ON Account.mm_address = mm_address.mm_addressId INNER JOIN
                     mm_turnover ON Account.mm_turnover = mm_turnover.mm_turnoverId INNER JOIN
                     Invoice ON Invoice.AccountId = Account.AccountId

WHERE mm_payment.mm_paymentstatus = 'Paid'; //  error 'invalid column name Payment_Status'

我还将您的表名添加到列中以便澄清。这也将解决在任何连接表中具有相同列名的潜在问题。

编辑:如果mm_paymentstatus是一个整数或位,试试这个,假设1是'付费':

WHERE mm_payment.mm_paymentstatus = 1

答案 1 :(得分:-1)

请使用 WHERE [付款状态],因为列名中有空格。所以你需要使用[]