Update with limitation and calculation : Update « Insert Delete Update « SQL / MySQL






Update with limitation and calculation

  
/*

mysql> select * from Orders;
+---------+--------+----------+---------------------+
| OrderID | BookID | Quantity | DateOrdered         |
+---------+--------+----------+---------------------+
|    1001 |    103 |        1 | 2004-01-12 12:30:00 |
|    1002 |    101 |        1 | 2001-02-12 12:31:00 |
|    1003 |    103 |        2 | 2002-03-12 12:34:00 |
|    1004 |    104 |        3 | 2003-04-12 12:36:00 |
|    1005 |    102 |        1 | 2004-05-12 12:41:00 |
|    1006 |    103 |        2 | 2001-06-12 12:59:00 |
|    1007 |    101 |        1 | 2002-07-12 13:01:00 |
|    1008 |    103 |        1 | 2003-08-12 13:02:00 |
|    1009 |    102 |        4 | 2004-09-12 13:22:00 |
|    1010 |    101 |        2 | 2005-11-12 13:30:00 |
|    1011 |    103 |        1 | 2006-12-12 13:32:00 |
|    1012 |    105 |        1 | 2001-02-12 13:40:00 |
|    1013 |    106 |        2 | 2002-04-12 13:44:00 |
|    1014 |    103 |        1 | 2003-06-12 14:01:00 |
|    1015 |    106 |        1 | 2005-01-12 14:05:00 |
|    1016 |    104 |        2 | 2003-11-12 14:28:00 |
|    1017 |    105 |        1 | 2002-03-12 14:31:00 |
|    1018 |    102 |        1 | 2001-05-12 14:32:00 |
|    1019 |    106 |        3 | 2003-07-12 14:49:00 |
|    1020 |    103 |        1 | 2004-01-12 14:51:00 |
+---------+--------+----------+---------------------+
20 rows in set (0.01 sec)

mysql> UPDATE Orders
    -> SET Quantity=Quantity+1
    -> WHERE BookID=103
    -> ORDER BY DateOrdered DESC
    -> LIMIT 5;
Query OK, 5 rows affected (0.03 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> select * from Orders;
+---------+--------+----------+---------------------+
| OrderID | BookID | Quantity | DateOrdered         |
+---------+--------+----------+---------------------+
|    1001 |    103 |        2 | 2005-10-09 08:51:26 |
|    1002 |    101 |        1 | 2001-02-12 12:31:00 |
|    1003 |    103 |        2 | 2002-03-12 12:34:00 |
|    1004 |    104 |        3 | 2003-04-12 12:36:00 |
|    1005 |    102 |        1 | 2004-05-12 12:41:00 |
|    1006 |    103 |        2 | 2001-06-12 12:59:00 |
|    1007 |    101 |        1 | 2002-07-12 13:01:00 |
|    1008 |    103 |        2 | 2005-10-09 08:51:26 |
|    1009 |    102 |        4 | 2004-09-12 13:22:00 |
|    1010 |    101 |        2 | 2005-11-12 13:30:00 |
|    1011 |    103 |        2 | 2005-10-09 08:51:26 |
|    1012 |    105 |        1 | 2001-02-12 13:40:00 |
|    1013 |    106 |        2 | 2002-04-12 13:44:00 |
|    1014 |    103 |        2 | 2005-10-09 08:51:26 |
|    1015 |    106 |        1 | 2005-01-12 14:05:00 |
|    1016 |    104 |        2 | 2003-11-12 14:28:00 |
|    1017 |    105 |        1 | 2002-03-12 14:31:00 |
|    1018 |    102 |        1 | 2001-05-12 14:32:00 |
|    1019 |    106 |        3 | 2003-07-12 14:49:00 |
|    1020 |    103 |        2 | 2005-10-09 08:51:26 |
+---------+--------+----------+---------------------+
20 rows in set (0.00 sec)


*/

Drop table Orders;

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


INSERT INTO Orders VALUES (1001, 103, 1, '2004-01-12 12:30:00'),
                          (1002, 101, 1, '2001-02-12 12:31:00'),
                          (1003, 103, 2, '2002-03-12 12:34:00'),
                          (1004, 104, 3, '2003-04-12 12:36:00'),
                          (1005, 102, 1, '2004-05-12 12:41:00'),
                          (1006, 103, 2, '2001-06-12 12:59:00'),
                          (1007, 101, 1, '2002-07-12 13:01:00'),
                          (1008, 103, 1, '2003-08-12 13:02:00'),
                          (1009, 102, 4, '2004-09-12 13:22:00'),
                          (1010, 101, 2, '2005-11-12 13:30:00'),
                          (1011, 103, 1, '2006-12-12 13:32:00'),
                          (1012, 105, 1, '2001-02-12 13:40:00'),
                          (1013, 106, 2, '2002-04-12 13:44:00'),
                          (1014, 103, 1, '2003-06-12 14:01:00'),
                          (1015, 106, 1, '2005-01-12 14:05:00'),
                          (1016, 104, 2, '2003-11-12 14:28:00'),
                          (1017, 105, 1, '2002-03-12 14:31:00'),
                          (1018, 102, 1, '2001-05-12 14:32:00'),
                          (1019, 106, 3, '2003-07-12 14:49:00'),
                          (1020, 103, 1, '2004-01-12 14:51:00');

select * from Orders;

UPDATE Orders
SET Quantity=Quantity+1
WHERE BookID=103
ORDER BY DateOrdered DESC
LIMIT 5;

select * from Orders;


           
         
    
  








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 records with calculation based on two tables
5.Update two tables with calculation
6.Using UPDATE Statements to Modify Data in Joined Tables
7.Update with condition
8.UPDATE statement includes a WHERE clause
9.An UPDATE statement can be qualified by the use of the ORDER BY clause and the LIMIT clause.
10.Update statement using table name alias
11.Update value by calculation
12.Update statement with variable (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
13.Update and order
14.Update with limit clause
15.UPDATE IGNORE
16.Update statement with case statement
17.Update statement with subquery (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
18.Updates all rows with an InStock value of less than 30
19.Adding Subqueries to Your UPDATE Statements
20.Updating a joined table
21.When you update values in a joined table, you can update more than one value at a time
22.Joining Tables in an UPDATE Statement
23.A different join is defined in the UPDATE clause.
24.SET clause uses the Quantity column to specify the new value for that column