Mục đích
Chỉ tăng giá trị Position thêm 10 cho các hàng có Position trong khoảng (50, 90), các hàng khác giữ nguyên.
1. Tạo bảng
mysql> create table DemoTable (
Name varchar(40),
Position int
);
Query OK, 0 rows affected (1.17 sec)
2. Chèn dữ liệu
mysql> insert into DemoTable values('Chris',90);
mysql> insert into DemoTable values('David',67);
mysql> insert into DemoTable values('Bob',55);
mysql> insert into DemoTable values('Sam',40);
3. Xem dữ liệu
mysql> select * from DemoTable;
+-------+----------+ | Name | Position | +-------+----------+ | Chris | 90 | | David | 67 | | Bob | 55 | | Sam | 40 | +-------+----------+ 4 rows in set (0.00 sec)
4. Cập nhật có điều kiện
mysql> update DemoTable
set Position = Position + 10
where Position > 50 and Position < 90;
Query OK, 2 rows affected (0.11 sec)
5. Kết quả
mysql> select * from DemoTable;
+-------+----------+ | Name | Position | +-------+----------+ | Chris | 90 | | David | 77 | | Bob | 65 | | Sam | 40 | +-------+----------+ 4 rows in set (0.00 sec)
Giải thích: Chỉ David (67→77) và Bob (55→65) được cập nhật. Chris (90) và Sam (40) không thay đổi.