Update records with calculation based on two tables : Update « Insert Delete Update « SQL / MySQL

SQL / MySQL
1. Backup Load
2. Command MySQL
3. Cursor
4. Data Type
5. Database
6. Date Time
7. Flow Control
8. Function
9. Insert Delete Update
10. Join
11. Key
12. Math
13. Procedure Function
14. Select Clause
15. String
16. Table Index
17. Transaction
18. Trigger
19. User Permission
20. View
21. Where Clause
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
SQL / MySQL » Insert Delete Update » Update 
Update records with calculation based on two tables

/*
mysql> select * from Books;
+--------+-----------+---------+
| BookID | BookName  | InStock |
+--------+-----------+---------+
|      1 | Poet      |    1934 |
|      2 | Ohio      |    1919 |
|      3 | Angels    |    1966 |
|      4 | Black     |    1932 |
|    101 | Writing   |      10 |
|    102 | News      |      17 |
|    103 | Angels    |      23 |
|    104 | Poet      |      32 |
|    105 | Dunces    |       6 |
|    106 | Solitude  |      28 |
|    107 | Postcards |    1992 |
|    108 | The       |    1993 |
+--------+-----------+---------+
12 rows in set (0.00 sec)

mysql> UPDATE Books, Orders
    -> SET Books.InStock=Books.InStock-Orders.Quantity
    -> WHERE Books.BookID=Orders.BookID
    ->    AND Orders.OrderID=1002;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from Books;
+--------+-----------+---------+
| BookID | BookName  | InStock |
+--------+-----------+---------+
|      1 | Poet      |    1934 |
|      2 | Ohio      |    1919 |
|      3 | Angels    |    1966 |
|      4 | Black     |    1932 |
|    101 | Writing   |       9 |
|    102 | News      |      17 |
|    103 | Angels    |      23 |
|    104 | Poet      |      32 |
|    105 | Dunces    |       6 |
|    106 | Solitude  |      28 |
|    107 | Postcards |    1992 |
|    108 | The       |    1993 |
+--------+-----------+---------+
12 rows in set (0.01 sec)


*/

Drop table Books;
Drop table Orders;
       
CREATE TABLE Books
(
   BookID SMALLINT NOT NULL PRIMARY KEY,
   BookName VARCHAR(40NOT NULL,
   InStock SMALLINT NOT NULL
)
ENGINE=INNODB;


CREATE TABLE Orders
(
   OrderID SMALLINT NOT NULL PRIMARY KEY,
   BookID SMALLINT NOT NULL,
   Quantity TINYINT (40NOT NULL DEFAULT 1,
   DateOrdered TIMESTAMP,
   FOREIGN KEY (BookIDREFERENCES Books (BookID)
)
ENGINE=INNODB;


INSERT INTO Orders VALUES (10011031'2004-01-12 12:30:00'),
                          (10021011'2001-02-12 12:31:00'),
                          (10031032'2002-03-12 12:34:00'),
                          (10041043'2003-04-12 12:36:00'),
                          (10051021'2004-05-12 12:41:00'),
                          (10061032'2001-06-12 12:59:00'),
                          (10071011'2002-07-12 13:01:00'),
                          (10081031'2003-08-12 13:02:00'),
                          (10091024'2004-09-12 13:22:00'),
                          (10101012'2005-11-12 13:30:00'),
                          (10111031'2006-12-12 13:32:00'),
                          (10121051'2001-02-12 13:40:00'),
                          (10131062'2002-04-12 13:44:00'),
                          (10141031'2003-06-12 14:01:00'),
                          (10151061'2005-01-12 14:05:00'),
                          (10161042'2003-11-12 14:28:00'),
                          (10171051'2002-03-12 14:31:00'),
                          (10181021'2001-05-12 14:32:00'),
                          (10191063'2003-07-12 14:49:00'),
                          (10201031'2004-01-12 14:51:00');


INSERT INTO Books VALUES (101'Writing', 12),
                         (102'News', 17),
                         (103'Angels', 23),
                         (104'Poet', 32),
                         (105'Dunces', 6),
                         (106'Solitude', 28);


select from Books;

UPDATE Books, Orders
SET Books.InStock=Books.InStock-Orders.Quantity
WHERE Books.BookID=Orders.BookID
   AND Orders.OrderID=1002;


select from Books;

           
       
Related examples in the same category
1. Do PLUS calculation in where clause
2. Assign value in select clause
3. Modifying Row Data
4. Update with limitation and calculation
5. Update two tables with calculation
6. Using UPDATE Statements to Modify Data in Joined Tables
7. Update with condition
w___w___w.___j__a___v_a___2___s__.___co__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.