Delete by JOIN : Delete « Insert Delete Update « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Insert Delete Update » Delete 
Delete by JOIN
    
/*
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.00 sec)

mysql> DELETE Orders.*
    -> FROM Books, Orders
    -> WHERE Books.BookID=Orders.BookID
    ->    AND Books.BookName='News';
Query OK, 3 rows affected (0.05 sec)

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 |
|    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 |
|    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 |
|    1019 |    106 |        3 | 2003-07-12 14:49:00 |
|    1020 |    103 |        1 | 2004-01-12 14:51:00 |
+---------+--------+----------+---------------------+
17 rows in set (0.00 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 Orders;

DELETE Orders.*
FROM Books, Orders
WHERE Books.BookID=Orders.BookID
   AND Books.BookName='News';

select from Orders;

           
         
    
    
    
  
Related examples in the same category
1.Deleting Rows with DELETE
2.Using DELETE LOW_PRIORITY command
3.Delete with JOIN 2
4.Using the 'from join delete' Alternative to Delete Data
5.Delete row with condition
6.Delete statement with subquery (ERROR 1093 (HY000): You can't specify target table 'EmployeeS' for update in FROM clause)
7.DELETE statement using the alternative.
8.Joining Tables in a DELETE Statement
9.Delete with where clause
10.Delete ignore
11.Adding Subqueries to Your DELETE Statements
12.Delete All Threads Except the Last 500
13.Delete rows from multiple tables
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.