Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

วิธีอัปเดตคอลัมน์ MySQL โดยการลบค่าพร้อมเงื่อนไข

มาสร้างตารางก่อน

mysql> create table DemoTable ( Score int ); Query OK, 0 rows affected (0.65 sec)

แทรกข้อมูลลงในตารางด้วยคำสั่ง insert

mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(29); Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(24); Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(32); Query OK, 1 row affected (0.09 sec)

แสดงข้อมูลทั้งหมดจากตารางด้วยคำสั่ง select

mysql> select * from DemoTable;

จะได้ผลลัพธ์ดังนี้

+-------+ | Score | +-------+ | 45 | | 29 | | 56 | | 24 | | 32 | +-------+ 5 rows in set (0.00 sec)

คำสั่งสำหรับอัปเดตคอลัมน์ MySQL โดยการลบค่าพร้อมเงื่อนไข

mysql> update DemoTable set Score=Score-2 where Score > 30; Query OK, 3 rows affected (0.14 sec) Rows matched: 3 Changed: 3 Warnings: 0

ตรวจสอบข้อมูลตารางอีกครั้ง

mysql> select * from DemoTable;

จะได้ผลลัพธ์ดังนี้

+-------+ | Score | +-------+ | 43 | | 29 | | 54 | | 24 | | 30 | +-------+ 5 rows in set (0.00 sec)