A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause : Limits « Select Clause « SQL / MySQL






A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause

      
mysql>
mysql>
mysql> CREATE TABLE Books
    -> (
    ->     BookID SMALLINT NOT NULL PRIMARY KEY,
    ->     BookName VARCHAR(40) NOT NULL,
    ->     InStock SMALLINT NOT NULL
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO Books
    -> VALUES (101, 'C: Writing on Writing', 12),
    -> (102, 'Oracle', 17),
    -> (103, 'Opera', 23),
    -> (104, 'Notebook', 32),
    -> (105, 'Pascal', 6),
    -> (106, 'One Hundred Years of Solitude', 28);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> 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;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO Orders VALUES (1001, 103, 1, '2004-10-12 12:30:00'),
    -> (1002, 101, 1, '2004-10-12 12:31:00'),
    -> (1003, 103, 2, '2004-10-12 12:34:00'),
    -> (1004, 104, 3, '2004-10-12 12:36:00'),
    -> (1005, 102, 1, '2004-10-12 12:41:00'),
    -> (1006, 103, 2, '2004-10-12 12:59:00'),
    -> (1007, 101, 1, '2004-10-12 13:01:00'),
    -> (1008, 103, 1, '2004-10-12 13:02:00'),
    -> (1009, 102, 4, '2004-10-12 13:22:00'),
    -> (1010, 101, 2, '2004-10-12 13:30:00'),
    -> (1011, 103, 1, '2004-10-12 13:32:00');
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> DELETE LOW_PRIORITY FROM Orders
    -> WHERE BookID=103
    -> ORDER BY DateOrdered DESC
    -> LIMIT 1;
Query OK, 1 row affected (0.00 sec)

mysql>
mysql>
mysql> drop table Orders;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table Books;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Limit the query results: Get 3 Most Expensive Products
2.Use LIMIT in SELECT
3.Use LIMIT
4.Another ORDER BY and limit
5.Simple LIMIT
6.Retrieving the Top Five Students
7.ORDER and limit
8.Limits and order
9.Scrolling through the entire product table four records at a time
10.Determining the Number of Records Suppressed by LIMIT (SQL_CALC_FOUND_ROWS, FOUND_ROWS)
11.Limiting a Selection Using LIMIT
12.Use ORDER BY to sort the result set. Then you can use LIMIT to find smallest and largest values.
13.Order the rows with the largest SUM( ) values first and use LIMIT to select the first record
14.The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,]
15.SELECT statement includes a LIMIT clause that specifies an offset value of 3 and a row count value of 4
16.WHERE clause can include additional logical operators and expressions that further limit the results returned
17.Limit to first three columns
18.LIMIT 5 OFFSET 3
19.Delete with order by and limit clause
20.Pulling a Section from the Middle of a Result Set
21.Tell MySQL what offset to use, from which result to start limiting.
22.The default offset is 0, so by specifying an offset of 1, you're taking the second record.
23.Selecting Records from the Beginning or End of a Result Set
24.Processing the First or Last n Records
25.Limiting the Number of Results
26.But LIMIT allows you to return only that record