2017-05-29 21 views
1

我想在此select語句中有if else條件。我要麼通過customerIDaccountID到這個映射器,因此我需要檢查哪一個通過。我有第一個案例沒有customerId(因此accountId被傳遞),所以內部連接被執行。第二個是沒有accountID(因此customerId通過)。然後在案件結束時,我想按降序排序。在SELECT中使用case時的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

可能是一個將表連接到case語句的新構造 – maSTAShuFu

回答

0

使用IFNULL

select ...,..., IFNULL(customerID,accountID) newid 
from 
    table1 
    left outer join table2 
order by date desc