php 用PHP清除MySQL表中的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/725982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear data in MySQL table with PHP?
提问by Powerlord
How do I clear all the entries from just one table in MySQL with PHP?
如何使用 PHP 从 MySQL 中的一个表中清除所有条目?
回答by Powerlord
TRUNCATE TABLE tablename
or
或者
DELETE FROM tablename
The first one is usually the better choice, as DELETE FROM is slow on InnoDB.
第一个通常是更好的选择,因为 DELETE FROM 在 InnoDB 上很慢。
Actually, wasn't this already answered in your other question?
实际上,这不是已经在您的另一个问题中回答了吗?
回答by jeffcook2150
TRUNCATE TABLE table;
is the SQL command. In PHP, you'd use:
是 SQL 命令。在 PHP 中,你会使用:
mysql_query('TRUNCATE TABLE table;');
回答by chaos
TRUNCATE TABLE `table`
unless you need to preserve the current value of the AUTO_INCREMENT sequence, in which case you'd probably prefer
除非您需要保留 AUTO_INCREMENT 序列的当前值,在这种情况下您可能更喜欢
DELETE FROM `table`
though if the time of the operation matters, saving the AUTO_INCREMENT value, truncating the table, and then restoring the value using
但如果操作时间很重要,保存 AUTO_INCREMENT 值,截断表,然后使用恢复值
ALTER TABLE `table` AUTO_INCREMENT = value
will happen a lot faster.
会发生得更快。
回答by Gary Carlyle Cook
MySQLI example where $con is the database connection variable and table name is: mytable.
MySQLI 示例,其中 $con 是数据库连接变量,表名是:mytable。
mysqli_query($con,'TRUNCATE TABLE mytable');
回答by ólafur Waage
TRUNCATE TABLE mytable
Be careful with it though.
不过要小心。
回答by raju gupta
TRUNCATE will blank your table and reset primary key DELETE will also make your table blank but it will not reset primary key.
TRUNCATE 将清空您的表格并重置主键 DELETE 也会使您的表格空白,但不会重置主键。
we can use for truncate
我们可以用于截断
TRUNCATE TABLE tablename
we can use for delete
我们可以用于删除
DELETE FROM tablename
we can also give conditions as below
我们也可以给出如下条件
DELETE FROM tablename WHERE id='xyz'
DELETE FROM tablename WHERE id='xyz'
回答by evan.stoddard
Don't mean to sound rude but this is something that can be easily answered with a quick google search. You should do that before you ask a community. w3schools.com is a great website for learning all this stuff. Here is a link that is a great tutorial for php and mysql. http://www.w3schools.com/php/php_mysql_intro.asp
并不是说听起来很粗鲁,但这可以通过快速谷歌搜索轻松回答。在询问社区之前,您应该这样做。w3schools.com 是一个学习所有这些东西的好网站。这是一个非常棒的 php 和 mysql 教程的链接。http://www.w3schools.com/php/php_mysql_intro.asp
回答by evan.stoddard
Actually I believe the MySQL optimizer carries out a TRUNCATE when you DELETE all rows.
实际上,我相信当您删除所有行时,MySQL 优化器会执行 TRUNCATE。

