簡體   English   中英

從SQL查詢返回特定數據,錯誤列名無效

[英]Return specific data from SQL query and error invalid column name

下面的SQL查詢返回我需要的數據,但是對於組織返回“付款狀態”,“待付款”和“已付款”表列,我希望為公司而不是以上返回“付款狀態”(已付款/未付款) 。

我以為這可能是where子句,其中付款狀態僅等於“已付款”和“未付款”,因此我試圖這樣做,但出現了錯誤

無效的列名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'

您的問題是您在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

請使用WHERE [付款狀態],因為列名中有空格。 所以你需要使用[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM