Search code examples
mysqldatabasetriggersphpmyadminmysql-error-1064

How to use SELECT with IF's condition for trigger in mysql?


I'm trying to create a trigger to change value of taille column if NULL.
I know another syntax error with freshman this is a rediculious thing. But, this level is so avanced than me.
You can begin reading the code from her :
#1064 - Erreur de syntaxe près de 'select i.id from categorie cat,souscategorie scat,item i,panier p where p.idite=' à la ligne 3.

delimiter /
create trigger voiddresstaille before insert on panier for each row
begin
    if new.idite=(select i.id from categorie cat,souscategorie scat,item i,panier p where p.idite=i.id and i.idscat=scat.id and scat.idcat=cat.id and cat.nom='Vêtements & Chaussures') then
        if new.taille='' || new.taille=NULL then
              new.taille='M';
        end if;
    end if;
end /

If you don't know the answer please vote up the quostion to help me. Thank you.


Solution

  • Using only 'select' commande is mean you can have a one or more line with your column. But, if you add 'limit 1' in the end of your commande you will get one line. The point here is, you can't comparing between one value and table of values. Only if you add to your condition 'in(select....etc)' to check your value in table's values.

    So that your code will be like this :

    delimiter /
    create trigger voiddresstaille before insert on panier for each row
    begin
        if new.idite in (select i.id from categorie cat,souscategorie scat,item i,panier p where p.idite=i.id and i.idscat=scat.id and scat.idcat=cat.id and cat.nom='Vêtements & Chaussures') then
            if new.taille='' || new.taille=NULL then
                  new.taille='M';
            end if;
        end if;
    end /