我在MySQL有下一张表。
id | aname | orderNum | parentId
1 | A | 2 | 1
2 | B | 1 | 1
3 | C | 4 | 2
4 | D | 1 | 2
5 | E | 3 | 2
6 | F | 2 | 2
7 | G | 3 | 1我想更新orderNum字段如下:
Set orderNum=1 when id=3 and parentId = 2
Set orderNum=2 when id=7 and parentId = 2
Set orderNum=3 when id=5 and parentId = 2
Set orderNum=4 when id=6 and parentId = 2 简而言之,我想按照ids的增量顺序(3、7、5、6)设置orderNum。我将按(3,7,5,6)顺序接收id,然后按相同的顺序更新orderNum字段。
有人能帮忙吗?如何为此创建更新查询?
我想用MySQL来做这个。
备注:请参阅更新的问题
发布于 2014-01-01 13:39:04
下面是sql查询,可以执行您想做的事情。
Update TABLENAME set `orderNum`= `id`;发布于 2014-01-01 13:39:25
使用字段()获取id序列中的id索引
update your_table
set ordernum = field(id, 3,7,5,6)
where id in (3,7,5,6)
and parentId = 2https://stackoverflow.com/questions/20869159
复制相似问题