2017-05-29 1 views
1

Je suis en train d'avoir une si la condition d'autre dans cette instruction select. Je suis soit en passant un customerID ou un accountID dans ce mappeur et donc j'ai besoin de vérifier lequel est passé. J'ai eu le premier cas où il n'y avait pas customerId (ainsi accountId est passé), et ainsi la jointure interne est effectuée. Le second est quand il n'y a pas accountID (ainsi customerId a été passé). Puis à la fin des cas, je veux le commander par dates décroissantes.erreur SQL Syntaxe lors de l'utilisation dans un cas SELECT

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 

Cependant, quand je lance, j'obtiens cette erreur.

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 

Est-ce que quelqu'un sait ce que je fais mal ici?

+0

Votre code n'a pas de sens. Par exemple, il n'y a pas de clause FROM. –

+0

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

+0

probablement une nouvelle construction de tables de jointure sur des instructions de cas – maSTAShuFu

Répondre

0

utilisation IFNULL

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