sql命令从数量中减去一个项目

时间:2013-12-01 09:16:58

标签: php mysql sql

我是我的新手,并不能真正弄清楚如何做到这一点。  我有两张桌子。  卡车

 select * from trucks;
+----+---------+----------+--------+--------------+
| id | size    | quantity | status | customers_id |
+----+---------+----------+--------+--------------+
| 12 | 10_feet | 3        | active |            0 |
| 13 | 10_feet | 3        | active |            0 |
| 14 | 10_feet | 2        | active |            0 |
| 15 | 14_feet | 5        | active |            0 |
| 16 | 14_feet | 2        | active |            0 |
| 17 | 14_feet | 2        | active |            0 |
| 18 | 17_feet | 2        | active |            0 |
| 19 | 17_feet | 2        | active |            0 |
| 20 | 24_feet | 3        | active |            0 |
| 21 | 10_feet | 1        | active |            0 |
| 22 | 24_feet | 1        | active |            0 |
+----+---------+----------+--------+--------------+

and truck_customers

select * from truck_customers;
+----+--------+----------+--------+------------------+---------+------------+
| id | first  | last     | dl     | email            | size    | date_in    |
+----+--------+----------+--------+------------------+---------+------------+
|  3 | Poul   | Jons     | A13324 | poul@gmail.com   | 10_feet | 2013-11-30 |
|  4 | Poul   | Watson   | A23439 | watson@gmail.com | 10_feet | 2013-11-30 |
|  5 | Alex   | Snders   | A22    | Alex@gmail.com   | 17_feet | 2013-11-30 |
|  6 | santes | Garsia   | A18337 | Santes@gmail.com | 10_feet | 2013-11-30 |
|  7 | James  | Bond     | JB111  | Bond@gmail.com   | 10_feet | 2013-11-30 |
|  8 | John   | Travolta | G123   | Gohn@gmail.com   | 14_feet | 2013-11-30 |
+----+--------+----------+--------+------------------+---------+------------+

在向truck_customers输入信息时,应自动减去 卡车表中的一件物品根据尺寸而定。

使用

$sql = " UPDATE trucks SET quantity = --  WHERE size = '$size'";

但它不起作用。

如果要更容易理解,我可以将代码发送给您。

2 个答案:

答案 0 :(得分:3)

你快到了。

UPDATE trucks SET quantity = quantity-1 WHERE size = '$size'

<强> Fiddle

答案 1 :(得分:0)

你的代码不仅不起作用,而且你也失去了你的WHERE条件......

-- this is a comment in SQL

http://dev.mysql.com/doc/refman/5.0/en/comments.html

您需要分配原始变量加/减增量,如上面提到的Hanky Panky。