语法格式
DELETE FROM table_name
[WHERE Clause]
用命令行删除数据
DELETE 语句
mysql> use RUNOON;
Database changed
mysql> DELETE FROM runoon_tbl WHERE runoon_id=3;
Query OK, 1 row affected (0.23 sec)
LIMIT 子句
DELETE FROM table_name
WHERE condition
ORDER BY colm1, colm2, …
LIMIT row_count;
mysql> DELETE FROM runoon_tbl ORDER BY runoon_title LIMIT 3;
JOIN 子句
mysql> DELETE Employees, Payment FROM Employees
INNER JOIN Payment
ON Employees.emp_id = Payment.emp_id
WHERE Employees.emp_id = 102;
使用 PHP 脚本删除数据
MySQL DELETE 语句测试
<?php
$dbhost = 'localhost'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
$sql = 'DELETE FROM runoon_tbl
WHERE runoon_id=3';
mysqli_select_db( $conn, 'RUNOON' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('无法删除数据: ' . mysqli_error($conn));
}
echo '数据删除成功!';
mysqli_close($conn);
?>
mysql> SELECT * FROM runoon_tbl WHERE runoon_id=3;
+-----------+---------------+---------------+-----------------+
| runoon_id | runoon_title | runoon_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 3 | 学习 Python | RUNOON.COM | 2016-05-06 |
+-----------+---------------+---------------+-----------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM runoon_tbl WHERE runoon_id=3;
Empty set (0.00 sec)
长按二维码,查阅更多教程
发表评论