MySQL delete from usage? delete statement in MySQL database

MySQL delete from usage?MySQL databasedelete statement

MySQL DELETE statement

You can use the SQL DELETE FROM command to delete records in a MySQL database table.

You can mysql> Execute the command from a command prompt or a PHP script.

grammar

The following is the general syntax of the SQL DELETE statement to delete data from a MySQL data table:

DELETE FROM table_name [WHERE Clause]
  • If no WHERE clause is specified, all records in the MySQL table will be deleted.
  • You can specify any condition in the WHERE clause
  • You can delete records all at once in a single table.

The WHERE clause is very useful when you want to delete specified records in the data table.


Delete data from the command line

Here we will use the WHERE clause in the SQL DELETE command to delete the selected data from the MySQL data table chenweiliang_tbl.

Examples

The following example will delete the record with chenweiliang_id 3 in the chenweiliang_tbl table:

SQL UPDATE statement:

mysql> use chenweiliang; Database changed mysql> DELETE FROM chenweiliang_tbl WHERE chenweiliang_id=3; Query OK, 1 row affected (0.23 sec)

Delete data using PHP script

PHP uses the mysqli_query() function to execute SQL statements, and you can use the WHERE clause with or without the SQL DELETE command.

This function works with mysql> The effect of the command character executing the SQL command is the same.

Examples

The following PHP example will delete the record with chenweiliang_id 3 in the chenweiliang_tbl table:

MySQL DELETE clause test:

<?
php
$dbhost = 'localhost:3306'; // 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 chenweiliang_tbl
 WHERE chenweiliang_id=3';
 
mysqli_select_db( $conn, 'chenweiliang' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
 die('无法删除数据: ' . mysqli_error($conn));
}
echo '数据删除成功!';
mysqli_close($conn);
?>

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "MySQL delete from usage? The delete statement in MySQL database" is helpful to you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-472.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top