MariaDB – 删除查询
MariaDB – 删除查询
DELETE 命令从指定的表中删除表行,并返回删除的数量。使用 ROW_COUNT() 函数访问删除的数量。WHERE 子句指定行,如果没有,则删除所有行。LIMIT 子句控制删除的行数。
在多行的 DELETE 语句中,它只删除满足条件的那些行;和 LIMIT 和 WHERE 子句是不允许的。DELETE 语句允许从不同数据库的表中删除行,但不允许从表中删除然后在子查询中从同一表中选择。
查看以下 DELETE 语法 –
DELETE FROM table_name [WHERE …]
从命令提示符或使用 PHP 脚本执行 DELETE 命令。
命令提示符
在命令提示符下,只需使用标准命令 –
root@host# mysql –u root –p password; Enter password:******* mysql> use PRODUCTS; Database changed mysql> DELETE FROM products_tbl WHERE product_id=133; mysql> SELECT * from products_tbl WHERE ID_number='133'; ERROR 1032 (HY000): Can't find record in 'products_tbl'
PHP 删除查询脚本
在 DELETE 命令语句中使用mysql_query()函数 –
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'DELETE FROM products_tbl WHERE product_id = 261'; mysql_select_db('PRODUCTS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete data: ' . mysql_error()); } echo "Deleted data successfully\n"; mysql_close($conn); ?>
成功删除数据后,您将看到以下输出 –
mysql> Deleted data successfully mysql> SELECT * from products_tbl WHERE ID_number='261'; ERROR 1032 (HY000): Can't find record in 'products_tbl'