在SELECT中使用案例时出现SQL语法错误

时间:2017-05-29 19:49:47

标签: mysql sql mybatis

我试图在这个select语句中有一个if else条件。我要么将customerIDaccountID传递给此映射器,因此我需要检查哪一个正在传递。我遇到了第一个没有customerId的情况(因此传递了accountId),因此执行了内连接。第二个是没有accountID时(因此customerId已通过)。然后在案例结束时,我想按降序日期订购。

    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

有谁知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

使用IFNULL

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