简体   繁体   English

在MYSQL / PHP中,如何从ID 1删除并将数据插入ID 5

[英]In MYSQL/PHP how would I delete from ID 1 and insert data to ID 5

在此处输入图片说明

This is how my table is setup. 这是我的表设置方式。 I want to add in a new height and then delete that data at ID 1 (2.0). 我想添加一个新的高度,然后删除ID为1(2.0)的数据。 So it keeps leap frogging when inserting in new data. 因此,在插入新数据时,它始终保持跳跃式增长。 This is so I can keep up to only 5 records at a time. 这样一来,我一次最多只能保留5条记录。

Sorry, I'm very new to MYSQL/PHP. 抱歉,我是MYSQL / PHP的新手。

Insert new data: 插入新数据:

INSERT INTO table (height) VALUES (xxx);

Delete the pair (1,2.0): 删除对(1,2.0):

DELETE FROM table WHERE id=1;

OR... all together like this: 或...全部像这样:

UPDATE table SET height=xxx WHERE id=1;

for the delete, something like this might work better: 对于删除,类似这样的方法可能会更好:

DELETE FROM table WHERE id NOT IN (SELECT ID FROM table ORDER BY ID DESC LIMIT 5)

essentially delete from the table where the id doesn't match the last 5 ids inserted. 本质上是从ID与插入的最后5个ID不匹配的表中删除。 This also works when there are less than 5 entries in the database, nothing would get deleted. 当数据库中的条目少于5个时,也不会删除任何条目,这也适用。

要删除第一项,请执行以下操作:

DELETE FROM table LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM