if in mysql insert statement

喜欢而已 提交于 2019-12-25 00:16:17

问题


I would like to do this kind of thing:

 IF ((19, 13) NOT IN (select idUtente, IdLezione from partecipa)) THEN
 insert into partecipa 
 (IdUtente, IdLezione, 
 IdAbbonamento, 
 utente,
  prova, riserva) 
 values( 
 19, 13, 
 (select idAbbonamento from abbonamento where attivo=1 and idUtente=19), 
 (select concat(u.nome," ",u.cognome) from abbonamento as a, utente as u where      a.attivo=1 and a.idUtente=u.idUtente and u.idUtente=19),
  0, 0)
 end if;

but there is an error in the syntax

Error Code: 1064. 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 'IF ((19, 13) NOT IN (select idUtente, IdLezione from partecipa)) THEN insert int' at line 1    0.000 sec 

and on internet I can't find any solution. Someone can help me?


回答1:


Try this:

INSERT INTO partecipa (IdUtente, 
                       IdLezione, 
                       IdAbbonamento, 
                       utente,  
                       prova, 
                       riserva)
SELECT 
  19, 
  13, 
  ab.idAbbonamento,  
  concat(u.nome, ' ', u.cognome), 
  0, 
  0
FROM abbonamento       AS ab 
INNER JOIN abbonamento AS a  ON ab.attivo   = a.attivo 
                            AND ab.idUtente = a.idUtente
INNER JOIN utente      AS u  ON a.idUtente  = u.idUtente 
WHERE a.attivo   = 1
  AND u.idUtente = 19
  AND NOT EXISTS(SELECT 1 
                 FROM partecipa 
                 WHERE idUtente  = 19 
                   AND IdLezione = 13);


来源:https://stackoverflow.com/questions/14960022/if-in-mysql-insert-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!