MySql在update语句中使用用户定义的变量

时间:2014-09-09 07:04:32

标签: mysql sql pdo

如何在MySql更新语句中使用用户定义的变量?根据用户定义的变量,我指的是@v:=

如果没有用户定义的变量,下面的语句可以正常工作,但不能使用它们。

update amga a left join amgb b on a.itemTempId = b.itemTempId
set @i:= concat(a.itemCountry,'-',a.id), a.itemId = @i, b.itemId = @i 
where a.itemId is null or b.itemId is null;

我稍后会在php PDO中使用它。

这样可行,但确实使用了用户定义的变量

update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = concat(a.itemCountry,'-',a.id), b.itemId = concat(a.itemCountry,'-',a.id) 
where a.itemId is null or b.itemId is null;

1 个答案:

答案 0 :(得分:2)

请尝试以下语法:

update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = (@i:= concat(a.itemCountry,'-',a.id)), b.itemId = @i 
where a.itemId is null or b.itemId is null;

SQLFiddle demo

相关问题