更改mysql中现有的重复条目 [英] changing existing duplicate entries in mysql
问题描述
我有一个表的位置,我想要的(可能)noob问题,但我是新的当插入新行时,更改位置低于插入行的位置的每一行的位置。例如,如果我添加位置4的行,如果有一个副本,它应该成为5,5应该转移到6等等...
还,有没有办法获得最高的列值,除了通过php测试它在每一行?
你需要两个查询。假设您知道您要插入的位置,请增加每个行位置大于或等于您插入的位置的位置:
更新表SET位置=位置+ 1 WHERE位置> = newPosition
之后可以插入 newPosition
,不存在重复项:
INSERT INTO表SET位置= newPosition
要获得最高价值,您可以使用 MAX()
SELECT MAX(position)FROM table
sorry for the (probably) noob question, but I', new at this stuff.
i have a table that has column 'position', and I want to, when inserting a new row, change positions of every row that has position lower than row that is inserted. for example, if I add row with position 4, if there is a duplicate, it should become 5, 5 should shift to 6 and so on...
also, is there a way to get highest value for column besides testing it in every row via php?
You need two queries. Assuming you know the position you're inserting, increase the position of each row that has position greater than or equal to the position you're inserting:
UPDATE table SET position = position + 1 WHERE position >= newPosition
After that, the newPosition
can be inserted and no duplicates will exist:
INSERT INTO table SET position = newPosition
To get the highest value, you can use MAX()
SELECT MAX(position) FROM table
这篇关于更改mysql中现有的重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!