2017-05-24 10 views
1

以下のSQLクエリは、必要なデータを返していますが、組織の "支払い状況"、 "支払い待ち"と "有料"の表の列に返されます。支払い状況(有料/未払い)は上記のものではなく企業向けに返されます。SQLクエリとエラー無効な列名から特定のデータを返します

私は、これは支払い状況は唯一の「有料」AND「未払い」に等しい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' 
+0

mm_payment.mm_paymentstatus' – Sariful

+1

SQLは、様々な条項という点で、かなり混乱した言語であるWHERE '試してみてください'SELECT'は明らかな(初心者の)順序では処理されません。ここにはSQL Server [ドキュメント](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql#logical-processing-order-of-the-select-文) –

答えて

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 
+0

これを試してみたところ、あいまいな列名 'mm_paymentstatus'というエラーが出ました。 –

+1

うん、私はそれが起こるかもしれないと思った。投稿を更新して修正しました。 –

+0

varchar値 'Paid'をデータ型intに変換すると、変換に失敗しました。 1行目 –

-1

[ペイを使用してくださいのみ(有料/無給)支払い状況を表示する方法を私ができるアドバイスしてくださいあなたは列名にスペースがあるので、[ステータス]をクリックします。したがって、[]を使用する必要があります

関連する問題