2017-05-29 1 views
1

このselectステートメントにif else条件を設定しようとしています。 customerIDまたはaccountIDをこのマッパーに渡しているので、どちらが渡されているかを確認する必要があります。 customerIdが存在しない(したがってaccountIdが渡された)最初のケースがあったため、内部結合が実行されました。第2のものは、accountIDが存在しない(したがってcustomerIdを通過した)ときである。その後、ケースの終わりに、日付を降順に並べ替えたいと思います。SELECT内でケースを使用するとSQL構文エラーが発生する

SELECT i.invoice_id, i.account_id, total_amount_due, due_date, bp.eff_date, bp.end_date, total_amount_due 
     (
     CASE 
      WHEN customerId = null 
       THEN INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id 
        WHERE account_id=#{accountId} 

      ELSE INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id 
       INNER JOIN server.cust_acct_rel car ON car.account_id = i.account_id 
        WHERE car.customer_id=#{customerId} 
     END)   
    ORDER BY end_date DESC 

ただし、これを実行すると、このエラーが発生します。

org.apache.ibatis.exceptions.PersistenceException: 
Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id WHERE ' at line 5 

私がここで間違っていることを知っている人はいますか?

+0

あなたのコードでは、意味がありません。例えば、 'FROM'節はありません。 –

+0

https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-クエリ – Strawberry

+0

おそらくケースステートメント上のテーブルを結合する新しい構造体 – maSTAShuFu

答えて

0

使用IFNULL

select ...,..., IFNULL(customerID,accountID) newid 
from 
    table1 
    left outer join table2 
order by date desc 
関連する問題