2013-02-19 124 views
0

我愿做这样的事情:如果MySQL的INSERT语句

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; 

,但有一个错误的语法

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 

和互联网上我找不到任何解决方案。有人可以帮助我吗?

+2

什么是错误讯息? – tuxtimo 2013-02-19 14:36:10

回答

2

试试这个:

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); 
+0

谢谢!现在它的工作;) – Martina 2013-02-19 14:52:38

+0

@ user1856906 - 不客气:)高兴我可以帮忙。 – 2013-02-19 15:09:34

+0

另一个问题,请... 如果我需要做这种事情,而不从其他表中选择值,我需要插入特定的值,如果该值不存在。 – Martina 2013-02-19 16:12:25