設置item_quantity後,您可以執行更新請求。然後你就可以直接比較item_quantity這樣的:
UPDATE `item_quantity`
SET
`quantity`= `quantity` - 5
,`requested`= IF(`quantity` = 0, 0, `requested`);
樣品
mysql> SELECT * FROM item_quantity;
+----+----------+-----------+
| id | quantity | requested |
+----+----------+-----------+
| 1 | 10 | 9 |
+----+----------+-----------+
1 row in set (0,00 sec)
mysql> UPDATE `item_quantity`
-> SET
-> `quantity`= `quantity` - 5
-> ,`requested`= IF(`quantity` = 0, 0, `requested`);
Query OK, 1 row affected (0,00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM item_quantity;
+----+----------+-----------+
| id | quantity | requested |
+----+----------+-----------+
| 1 | 5 | 9 |
+----+----------+-----------+
1 row in set (0,00 sec)
mysql> UPDATE `item_quantity`
-> SET
-> `quantity`= `quantity` - 5
-> ,`requested`= IF(`quantity` = 0, 0, `requested`);
Query OK, 1 row affected (0,00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM item_quantity;
+----+----------+-----------+
| id | quantity | requested |
+----+----------+-----------+
| 1 | 0 | 0 |
+----+----------+-----------+
1 row in set (0,00 sec)
mysql>
我試過,但從IF開始,以分號總是紅色下劃線。我猜我的語法錯了。我如何解決它?它只是說意想不到的標記 – healer
@healer - 您能否請您發佈您的查詢 –
@healer - 您的查詢中必須有錯字。我在我的回答中添加了一個示例 –