mysql delete Multi table Join Deletion Function

  • 2021-07-10 21:01:58
  • OfStack

Deletion of a single table:


DELETE FROM tableName WHERE columnName = value;
 Delete all rows in the table: 
 That is, keep the structure, attributes and indexes of the table 
DELETE FROM tablename;
DELETE * FROM tablename;

Delete all contents in the same table (delete data and table structure)


TRUNCATE customer;

Unable to report several rows deleted, and can only be used for single 1 tables

Multiple table join deletion:


DELETE orders,itrms FROM orders,items 
  WHERE orders.userid = items.userid
  AND orders.orderid = items.orderid
  AND orders.date<"2000/03/01"; 

The name column of the table to be deleted is after DELETE, and the table column used by the join condition is after FROM

It is assumed that all wineries in the BV area are deleted, but the place names are not deleted


DELETE winery FROM region,winery 
  WHERE winery.regionid = region.regionid
  AND region.regionname = 'BV';

The query only affects the winery table, but at the same time, it uses winery and region to find out the records to be deleted

Using advanced join queries


DELETE orders,items FROM orders
  INNER JOIN otems ON orders.orderid = items.orderid
  AND orders.userid = items.userid
  WHERE orders.date<"2000/03/01";  

You can also use nested queries (internal queries cannot reference deleted data), GROUP, BY, HAVING in DELETE statements;

You can also use ORDER BY in a single 1 table query, and singular numbers don't make much sense unless they are used with LIMIT to delete some data rows.

Add quick modifier to quickly delete index entries and accelerate large or frequent deletion operations


DELETE QUICK FROM customer WHERE userid<10;

Available only for tables of type MyISAM

Cleaning the tables of MyISAM


OPTIMIZE TABLE customer;

Related articles: