2014-09-09 1 views
1

MySql 업데이트 문에서 사용자 정의 변수를 어떻게 사용할 수 있습니까? 사용자가 정의한 변수에 따라 @v:=을 참조합니다.업데이트 문에서 사용자 정의 변수를 사용하는 MySql

아래의 명령문은 사용자 정의 변수가 없어도 정상적으로 작동하지만 그 값은 사용할 수 없습니다.

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; 

답변

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

+0

감사합니다. 이것은 잘 작동합니다. – Norman

+1

MySQL 업데이트가 다음 번에 업데이트 된 값을 사용하기 때문에'set a.itemId = concat (a.itemCountry, '-', a.id), b.itemId = a.itemId'와 함께 사용자 변수 AFAIK를 피할 수 있습니다. "SET"과제 – Xenos

관련 문제