首页 > 解决方案 > 如何使用 SELECT 和 IF 条件在 mysql 中触发?

问题描述

如果为NULL,我正在尝试创建一个触发器来更改taille 列的值。
我知道大一新生的另一个语法错误,这是一件可笑的事情。但是,这个水平比我高太多了。
你可以从她那里开始阅读代码:
#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 /

如果您不知道答案,请投票支持我。谢谢你。

标签: mysqldatabasetriggersphpmyadminmysql-error-1064

解决方案


你的语法是错误的。

我将您的查询更改为正确的内部连接

只能从您的选择中获得一个 i.id,否则这会导致错误

DROP TRIGGER IF EXISTS voiddresstaille ;
delimiter //
create trigger voiddresstaille before insert on panier for each row
begin
    if new.idite = (select i.id 
                    from categorie cat 
                    INNER JOIN souscategorie scat ON scat.idcat=cat.id 
                    INNER JOIN item i ON i.idscat=scat.id 
                    INNER JOIN panier p ON p.idite=i.id 
                    where  cat.nom='Vêtements & Chaussures') then
            if new.taille='' OR new.taille IS NULL then
               SET new.taille='M';
            end if;
    end if;
end //
DELIMITER ;

推荐阅读