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



我试图在这个选择语句中有一个 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

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

使用 IFNULL

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

相关内容

最新更新