开发者

Store the order of something in MySQL

开发者 https://www.devze.com 2023-01-23 10:02 出处:网络
Let\'s say I have a table that\'s like the following: name VARCHAR(50) PRIMARY KEY NOT NULL ordernum TINYINT UNSIGNED NOT NULL

Let's say I have a table that's like the following:

name VARCHAR(50) PRIMARY KEY NOT NULL
ordernum TINYINT UNSIGNED NOT NULL

Also let's say I have 3 rows.

something     -开发者_如何学编程 1
somethingelse - 2
somethingmore - 3

If i want to insert another entry called something1, and give it an ordernum of 2, how do I reorder all the rows that has an ordernum of 2 or greater? i.e. somethingelse becomes has an ordernum of 3, and somethingmore has an ordernum of 4?

I've heard the you can do it via FIELD(), but I'm unsure how to exactly.

If it's not possible, anywy to do it in PHP?


In your example:

UPDATE `table` SET `ordernum` = `ordernum` + 1 WHERE `ordernum` >= 2
INSERT INTO `table` (`name`, `ordernum`) VALUES ('something1', 2)
0

精彩评论

暂无评论...
验证码 换一张
取 消