我正在尝试运行这个:
UPDATE test
SET col2=1 WHERE col1='test1',
SET col2=3 WHERE col1='test2';
我得到的错误:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
我的 table :
CREATE TABLE `test` (
`col1` varchar(30) NOT NULL,
`col2` int(5) DEFAULT NULL,
PRIMARY KEY (`col1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
这是关于第一行末尾的 ,
的东西。当我将其更改为 ;
时,它无法识别 col2。我怎样才能在一个查询中做到这一点?
最佳答案
这是最清晰的方法
UPDATE test
SET col2 = CASE col1
WHEN 'test1' THEN 1
WHEN 'test2' THEN 3
WHEN 'test3' THEN 5
END,
colx = CASE col1
WHEN 'test1' THEN 'xx'
WHEN 'test2' THEN 'yy'
WHEN 'test3' THEN 'zz'
END
WHERE col1 IN ('test1','test2','test3')
关于mysql - 在单个 MySQL 查询中更新多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802671/