在MySql中切换两行的ID号 [英] Switch id numbers of two rows in MySql

查看:166
本文介绍了在MySql中切换两行的ID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP切换mysql中的两行的id - 我读得越多,我就越困惑。似乎有很多冲突的信息。

I am trying to switch the id's of two rows in mysql using php - the more i read about it the more confused i get. There seems to be a lot of conflicting information. Has anybody got a definitive answer.

例如在初始状态下,我的行是

eg in initial state my rows are

1-Peter-22-germany
2-mary-16-iceland
3-tom-29-france
4-michael-34-greece

然后我想交换行2和3的id,使它看起来像这样

and then i would like to swap the id's of rows 2 and 3 so that it would look like this

1-Peter-22-germany
3-mary-16-iceland
2-tom-29-france
4-michael-34-greece

p>

so that if i then ordered it by id i would have

1-Peter-22-germany
2-tom-29-france
3-mary-16-iceland
4-michael-34-greece


推荐答案

如果ID是主键,那么您不想更改它/可以更改它。如果这仅用于排序,我建议创建一个订单列整数和排序。表模式很好看,但这是我迄今为止的建议。

If the ID is the primary key then you don't want to change it/be able to change it. If this is only for sorting, I would suggest making an "order" column that is an integer and sorting on that. Table schema would be nice to see for this but this is my recommendation so far.

如果ID是在输入行时自动创建的,那么这个想法是一个更大的问题。但是,现在我只是推测,因为我不知道你的表模式。

If ID is created automatically when rows are entered then this idea is an even larger problem. However, now I'm just speculating as I don't know your table schema.

这里是一些关于主键的标准建议:不要想作为唯一标识符以外的任何内容。

Here is some standard advice on primary keys: Do not think of them as anything except a unique identifier.

这篇关于在MySql中切换两行的ID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆