MySQL 使用嵌套的 select 语句触发

MySQL Trigger with nested select statements

我在使用 magento 数据库中的触发器语法时遇到一些困难。

我想在创建新地址时更新客户 table 中的值。

customer_address_entity_varchar table 看起来像这样:

value_id | entity_type_id | attribute_id | entity_id | value
48       | 2              | 28           | 3         | Lancashire

当我在没有嵌套 select 语句的情况下执行触发器时它起作用了,所以:

IF (new.attribute_id=28) THEN
UPDATE customer_entity SET customer_entity.group_id = 4
WHERE customer_entity.entity_id = 1
END IF;

我正在尝试使用 new.entity_id 和 new.value 到其他 table 中的 select 值来替换 4 和 1:

DROP TRIGGER IF EXISTS `groupid_update`;
CREATE DEFINER=`rock_store`@`localhost` TRIGGER `groupid_update` 
AFTER INSERT ON `customer_address_entity_varchar` 

FOR EACH ROW 

IF (new.attribute_id = 28) THEN 

UPDATE customer_entity 
SET customer_entity.group_id = ( 
    SELECT directory_country_region_name.customer_group_id 
    FROM directory_country_region_name 
    WHERE directory_country_region_name.name = "'" + new.value + "'") 
WHERE customer_entity.entity_id = ( 
    SELECT customer_address_entity.parent_id 
    FROM customer_address_entity 
    WHERE customer_address_entity.entity_id = new.entity_id); 
END IF;

在 MySQL 中创建触发器是成功的,但是当我尝试在 Magento 中添加新地址时,我收到一个错误,没有任何提示。

感谢您的帮助。利亚姆

我通过分解我试图做的事情来设法解决这个问题。

IF (new.attribute_id = 28) THEN 

SET @add_entity_id = new.entity_id;
SET @county = new.value;


SET @group_id = (SELECT directory_country_region_name.customer_group_id 
FROM directory_country_region_name 
WHERE directory_country_region_name.name = @county);

SET @customer_id = ( 
SELECT customer_address_entity.parent_id 
FROM customer_address_entity 
WHERE customer_address_entity.entity_id = new.entity_id);

UPDATE customer_entity SET customer_entity.group_id = @group_id WHERE customer_entity.entity_id = @customer_id;

END IF