我有一个表ListLocations,其中包含列Name,StateID,CountryID和DisplayName。 stateid指的是列出美国/领土及其缩写的表格,countryid指的是国家及其缩写的表格,displayname是我想要显示的字符串,是name和州/国家缩写的组合。
为了实现displayname,我有一个存储过程,它在插入后/更新后运行locations表,如下所示-CREATE DEFINER=`root`@`%` PROCEDURE `updateLocationDisplayName`(in locationId int, in stateId int, in countryId int)
BEGIN
IF (stateId = -1 AND countryId = -1) OR (countryId = 220 and stateId = -1)
THEN
UPDATE listlocations
SET displayName = name
WHERE idx = locationId;
ELSEIF (countryId = 220 and stateId !=-1)
THEN
UPDATE listlocations ll
LEFT JOIN listunitedstates us ON us.idx = ll.stateIdx
SET displayName = CONCAT(ll.name, ', ', us.abbreviation)
WHERE idx = locationId;
ELSEIF (countryId != -1 AND countryId != 220)
THEN
UPDATE listlocations ll
LEFT JOIN listcountries lc ON lc.idx = ll.countryIdx
SET displayName = CONCAT(ll.name, ', ', lc.abbreviation)
WHERE idx = locationId;
END IF;
END
但是现在,尝试更新listlocations时,我得到错误代码:1442。 无法更新存储函数/触发器中的表“listlocations”,因为调用此存储函数/触发器的语句已经使用了该表。
在MySQL5.6中处理这种情况的“正确”方法是什么--您有一列是根据其他列计算的,并且您希望它在插入/更新时保持更新(并且在DB本身中处理,而不是在应用程序代码中处理)?