我对根据行索引或计数删除记录有点困惑。假设我有这三行:
ID | userId | postID
15 1 10
16 1 10
17 1 10
这些数据是使用查询获取的:
SELECT * FROM postimages where postID=10
我想根据名为 index
的变量中的索引删除其中一条记录。索引可以是 1,2 或 3,因此查询可能应该如下所示:
delete from postimages where postID=10 -- here I'm stuck
可不可以?
最佳答案
是的,这是可能的,只需使用limit-offset
,如下所示:
delete from postimages where postID=10 limit 1 offset 1
此查询将删除 1 行,因为限制 1
,
此行将是第二行,因为偏移 1
。
要删除第一行 - 使用limit 1 offset 0
,
删除第二行 - 使用limit 1 offset 1
,
要删除 3d 行 - 使用limit 1 offset 2
等等。
免责声明:为了获得可靠的体验,您必须指定 order by
子句,例如:
delete from postimages where postID=10 order by ID limit 1 offset 1
关于MYSQL - 删除行索引中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52648357/