ใช้ AND ระบุเงื่อนไขหลายคอลัมน์ใน UPDATE
สร้างตารางและเพิ่มข้อมูล
mysql> create table DemoTable1616 ( StudentId int, StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.44 sec)
mysql> insert into DemoTable1616 values(101,'Chris',56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1616 values(102,'Bob',87); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1616 values(103,'David',56); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1616 values(101,'Bob',89); Query OK, 1 row affected (0.14 sec)
คิวรี่อัปเดตเฉพาะ StudentId=101 และ StudentMarks=56
mysql> update DemoTable1616 set StudentName='Adam' where StudentId=101 and StudentMarks=56; Query OK, 1 row affected (0.48 sec) Rows matched: 1 Changed: 1 Warnings: 0
ผลลัพธ์หลังอัปเดต
mysql> select * from DemoTable1616;
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 101 | Adam | 56 | | 102 | Bob | 87 | | 103 | David | 56 | | 101 | Bob | 89 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
มี Id 101 สองแถว แต่อัปเดตเฉพาะแถวที่ Marks=56 | แถวที่ Marks=89 ไม่เปลี่ยน | ใช้ AND เพื่อความแม่นยำ