删除mysql第n行

发布于 2024-10-20 10:35:57 字数 673 浏览 3 评论 0原文

我正在使用 MySQL v5.0.45 并尝试删除表中的第 n 行,无论其 ID 号如何。我在 PHP 中设置了一个原型,然后将其集成到我的 Web 开发项目中,在数据库中设置了以下内容:

CREATE TABLE prototype_1 (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name varchar(30));

INSERT INTO prototype_1 (name) VALUES ('A');
INSERT INTO prototype_1 (name) VALUES ('B');
INSERT INTO prototype_1 (name) VALUES ('C');

足够简单!现在我尝试使用“LIMIT”,但收到以下错误:

“ERROR 1235 (42000): This version of MySQL does not support 'LIMIT & IN/ALL/ANY/SOME subquery'”

现在我明白你不能使用然而,子查询中的 LIMIT 是否没有某种解决方法?

我可以选择第 n 行并通过以下内容很好地显示它:

SELECT * FROM prototype_1 LIMIT 1,1;

因此返回 'B' 但为什么我不能删除!?

I'm using MySQL v5.0.45 and trying to delete the nth row in a table irrespective of its ID number. I set up a prototype in PHP before integrating it into my web development project where I set up the following in my database:

CREATE TABLE prototype_1 (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name varchar(30));

INSERT INTO prototype_1 (name) VALUES ('A');
INSERT INTO prototype_1 (name) VALUES ('B');
INSERT INTO prototype_1 (name) VALUES ('C');

Simple enough! Now I tried using 'LIMIT' but I get the following error:

"ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"

Now I understand you cannot use LIMIT in subqueries, however, is there not some sort of work around?

I can select the nth row and display it nicely with the following:

SELECT * FROM prototype_1 LIMIT 1,1;

Thus returning 'B' but why cannot I delete!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临走之时 2024-10-27 10:35:57

你需要一个像这样的小技巧

delete from prototype_1 where id = (select id from (select id from prototype_1 order by id limit 1,1) as t)

来自 docs

LIMIT 子句可用于限制 SELECT 语句返回的行数。 LIMIT 接受一个或两个数字参数,它们必须都是非负整数常量

(...)

有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):

You need a little trick like this

delete from prototype_1 where id = (select id from (select id from prototype_1 order by id limit 1,1) as t)

From docs

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants

(...)

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

野の 2024-10-27 10:35:57

像这样删除第 n 条记录。确保按所需的列对结果进行排序,在本例中为 id 列:

DELETE FROM prototype_1 ORDER BY id LIMIT n-1,1

Delete the n-th record like this. Make sure you order the result by the column you want, in this case the id column:

DELETE FROM prototype_1 ORDER BY id LIMIT n-1,1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文