简体   繁体   English

my_sql,需要删除第一行

[英]my_sql, need to delete 1st row

I've looked this up here and on the web in general and not found an answer.我已经在此处和 web 上查找了此内容,但没有找到答案。

For reasons I'll not go into, I need to delete the top row from a mysql database table, then refresh the page.由于我不会将 go 放入的原因,我需要从 mysql 数据库表中删除顶行,然后刷新页面。

The delete is always for the top row, and independent of the row's content, so doing a select where is useless.删除始终针对顶行,并且与行的内容无关,因此执行 select 是没有用的。

Seems to me I should be able to call (in my php code) this:在我看来,我应该能够调用(在我的 php 代码中)这个:

mysql_deleterow(0);  // delete the top row from the table

and be done with it.并完成它。 But oh no -- no such call that I can find.但是哦,不——我找不到这样的电话。

Is there a way to just delete a table row independent of its content?有没有办法只删除一个独立于其内容的表行? (please reread above where I say 'for reasons I'll not go into' if you feel an urge to ask me 'Why do you want to do that?') (如果你想问我“你为什么要这样做?”,请重新阅读上面我说的“我不会 go 进入”的原因)

Will it work for you: DELETE FROM your_table [ORDER BY field_name] LIMIT 1;它对你有用吗: DELETE FROM your_table [ORDER BY field_name] LIMIT 1;

You can omit ORDER BY .您可以省略ORDER BY

You can use this query to delete the row from the database:您可以使用此查询从数据库中删除该行:

mysql_query('DELETE FROM table_name LIMIT 1');

To refresh the page:刷新页面:

header("Refresh: 0; url=".$_SERVER['PHP_SELF']);

You could try DELETE FROM table LIMIT 1 .您可以尝试DELETE FROM table LIMIT 1 If this doesn't do what you want, you can do something like DELETE FROM table WHERE id=(SELECT id FROM table ORDER BY XXXX ASC LIMIT 1) LIMIT 1 .如果这不能满足您的要求,您可以执行DELETE FROM table WHERE id=(SELECT id FROM table ORDER BY XXXX ASC LIMIT 1) LIMIT 1之类的操作。 This second way will allow you to control which row is the top one.第二种方式将允许您控制哪一行是第一行。

There is no such thing as a "top row".没有“顶行”之类的东西。 You will need to define what that is yourself, for example the one with the lowest id, the oldest timestamp or similar (and you will need to create those fields yourself as well).您将需要自己定义它,例如具有最低 id、最旧时间戳或类似的那个(您还需要自己创建这些字段)。

If we define the top row as the one with the lowest id, we can delete it like this:如果我们将第一行定义为 id 最低的那一行,我们可以像这样删除它:

DELETE FROM table ORDER BY id ASC LIMIT 1;

You can also do this without the ORDER BY clause, but in that case the row which gets deleted is undefined.您也可以在没有 ORDER BY 子句的情况下执行此操作,但在这种情况下,被删除的行是未定义的。 This may or may not delete the "top row":这可能会也可能不会删除“顶行”:

DELETE FROM table LIMIT 1;

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

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