1

I am new in my sql and can not really figure out how to do it. I have a two tables. trucks

 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 |
+----+--------+----------+--------+------------------+---------+------------+

When entering information into truck_customers, it should automatically subtract one item from trucks table based on the size.

Using

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

But it does not work.

I can send you my code if it is going to be easier to understand.

2 Answers2

3

You're almost there.

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

Fiddle

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

Not only would your code not work, but you'd lose your WHERE conditions too...

-- this is a comment in SQL

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

You need to assign to original variable plus/minus increment, as Hanky Panky mentioned above.

scrowler
  • 24,273
  • 9
  • 60
  • 92