mysql更新部分多行,在单个MySQL查询中更新多行

I am trying to run this:

UPDATE test

SET col2=1 WHERE col1='test1',

SET col2=3 WHERE col1='test2';

The error I am getting:

[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 '

My table:

CREATE TABLE `test` (

`col1` varchar(30) NOT NULL,

`col2` int(5) DEFAULT NULL,

PRIMARY KEY (`col1`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

It’s something about , at the end of the first row. When I changed it to ;, it didn’t recognize col2. How can I do this in one query?

解决方案

This is most clear way

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值