mysql 查询 更新_MySQL-更新查询

mysql 查询 更新

mysql 查询 更新

MySQL-更新查询 (MySQL - UPDATE Query)

There may be a requirement where the existing data in a MySQL table needs to be modified. You can do so by using the SQL UPDATE command. This will modify any field value of any MySQL table.

可能需要修改MySQL表中的现有数据。 您可以通过使用SQL UPDATE命令来实现。 这将修改任何MySQL表的任何字段值。

句法 (Syntax)

The following code block has a generic SQL syntax of the UPDATE command to modify the data in the MySQL table −

以下代码块具有UPDATE命令的通用SQL语法,用于修改MySQL表中的数据-


UPDATE table_name SET field1 = new-value1, field2 = new-value2
[WHERE Clause]

  • You can update one or more field altogether.

    您可以总共更新一个或多个字段。
  • You can specify any condition using the WHERE clause.

    您可以使用WHERE子句指定任何条件。
  • You can update the values in a single table at a time.

    您可以一次更新一个表中的值。

The WHERE clause is very useful when you want to update the selected rows in a table.

当您要更新表中的选定行时,WHERE子句非常有用。

从命令提示符更新数据 (Updating Data from the Command Prompt)

This will use the SQL UPDATE command with the WHERE clause to update the selected data in the MySQL table tutorials_tbl.

这将使用带有WHERE子句SQL UPDATE命令来更新MySQL表tutorials_tbl中的选定数据。

(Example)

The following example will update the tutorial_title field for a record having the tutorial_id as 3.

以下示例将更新tutorial_id为3的记录的tutorial_title字段。


root@host# mysql -u root -p password;
Enter password:*******

mysql> use TUTORIALS;
Database changed

mysql> UPDATE tutorials_tbl 
   -> SET tutorial_title = 'Learning JAVA' 
   -> WHERE tutorial_id = 3;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

使用PHP脚本更新数据 (Updating Data Using a PHP Script)

You can use the SQL UPDATE command with or without the WHERE CLAUSE into the PHP function – mysql_query(). This function will execute the SQL command in a similar way it is executed at the mysql> prompt.

您可以在SQL函数mysql_query()中使用带有或不带有WHERE子句SQL UPDATE命令。 该函数将以与在mysql>提示符下执行的类似方式执行SQL命令。

(Example)

The following example to update the tutorial_title field for a record having tutorial_id as 3.

以下示例更新了tutorial_id为3的记录的tutorial_title字段。


<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'UPDATE tutorials_tbl
      SET tutorial_title="Learning JAVA"
      WHERE tutorial_id=3';

   mysql_select_db('TUTORIALS');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not update data: ' . mysql_error());
   }
   echo "Updated data successfully\n";
   mysql_close($conn);
?>

翻译自: https://www.tutorialspoint.com/mysql/mysql-update-query.htm

mysql 查询 更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值