首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对指定的多个id依次更新order字段的值?

问如何对指定的多个id依次更新order字段的值?
EN

Stack Overflow用户
提问于 2014-01-01 13:35:56
回答 2查看 127关注 0票数 1

我在MySQL有下一张表。

代码语言:javascript
复制
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字段如下:

代码语言:javascript
复制
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来做这个。

备注:请参阅更新的问题

EN

回答 2

Stack Overflow用户

发布于 2014-01-01 13:39:04

下面是sql查询,可以执行您想做的事情。

代码语言:javascript
复制
Update TABLENAME set `orderNum`= `id`;
票数 1
EN

Stack Overflow用户

发布于 2014-01-01 13:39:25

使用字段()获取id序列中的id索引

代码语言:javascript
复制
update your_table
set ordernum = field(id, 3,7,5,6) 
where id in (3,7,5,6)
and parentId = 2
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20869159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档