JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Deleting using Join in MySQL

Author:JIYIK Last Updated:2025/04/21 Views:

This tutorial article will show you how to use MySQL JOINmethods to delete data from multiple tables. This is useful when you are simultaneously deleting records from one table that are related to a specific record in another table.


DELETE FROMLimitations of using the statement in MySQL

DELETE FROMThe statement is used to delete rows from a table. You can apply WHEREto specify the rows and conditions to be met when deleting.

The following code contains two tables, the first records the vendor's products and their locations, and the second records the list of store owners who sell these products:

Create Table supplier (
    suppId INTEGER PRIMARY KEY,
    category TEXT NOT NULL,
    location TEXT NOT NULL
);
Create Table shops (
    owner TEXT NOT NULL,
    shptype TEXT NOT NULL,
    area TEXT NOT NULL
);

INSERT INTO supplier VALUES (0001, 'Veggies', 'Downtown');
INSERT INTO supplier VALUES (0002, 'Fruits', 'Downtown');
INSERT INTO supplier VALUES (0003, 'Dairy', 'Downtown');
INSERT INTO supplier VALUES (0004, 'Veggies', 'Uptown');
INSERT INTO supplier VALUES (0005, 'Fruits', 'Uptown');
INSERT INTO supplier VALUES (0006, 'Dairy', 'Uptown');

INSERT INTO shops VALUES ('Mark', 'Veggies', 'Downtown');
INSERT INTO shops VALUES ('Mark', 'Fruits', 'Downtown');
INSERT INTO shops VALUES ('Susan', 'Dairy', 'Downtown');

All the stores in the table are located in the city center, so in this case Uptownit does not make sense to maintain supplier records for the region.

You can use it DELETEon only one table at a time. You can shopsget data from the table, specify conditions, and then delete supplierall records in the table that match all the conditions.

In this case, we will use delete all the records JOINfrom supplierthe table .Uptown


INNER JOINUsing the method in MySQL

Using INNER JOINthe method will allow you to merge two or more tables and delete multiple rows at once.

There are other options, such as using a subquery, but this is a more practical way to get the job done. Using the example tables above, you can delete Uptownall rows that have a value of in both tables.

The following code will find the location of the store and keep the suppliers in Downtownthe region, while removing Uptownthe supplier registered as :

DELETE supplier FROM supplier
INNER JOIN shops ON supplier.category = shops.shptype
WHERE supplier.location != shops.location;

The tables are joined based on fields that share similar data. The Category field shows in this case what the supplier supplied and which stores sold it.

Merging the two tables will allow you to define only one deletion condition. In this case, the supplier's position will determine which rows to delete.

You can also DELETEdelete both tables at the same time by adding the table name after . This will work if you have multiple records in multiple tables.

DELETE supplier, shops FROM supplier
...

JOINThe final result of deleting MySQL using

The result will keep your table intact while removing all records for suppliers that do not meet the above criteria.

Create Table supplier (
    suppId INTEGER PRIMARY KEY,
    category TEXT NOT NULL,
    location TEXT NOT NULL
);
Create Table shops (
    owner TEXT NOT NULL,
    shptype TEXT NOT NULL,
    location TEXT NOT NULL
);

INSERT INTO supplier VALUES (0001, 'Veggies', 'Downtown');
INSERT INTO supplier VALUES (0002, 'Fruits', 'Downtown');
INSERT INTO supplier VALUES (0003, 'Dairy', 'Downtown');
INSERT INTO supplier VALUES (0004, 'Veggies', 'Uptown');
INSERT INTO supplier VALUES (0005, 'Fruits', 'Uptown');
INSERT INTO supplier VALUES (0006, 'Dairy', 'Uptown');

INSERT INTO shops VALUES ('Mark', 'Veggies', 'Downtown');
INSERT INTO shops VALUES ('Mark', 'Fruits', 'Downtown');
INSERT INTO shops VALUES ('Susan', 'Dairy', 'Downtown');
-- delete function 
DELETE supplier FROM supplier
INNER JOIN shops ON supplier.category = shops.shptype
WHERE supplier.location != shops.location;

The above matches all suppliers to a store by location, in this case Downtown, and removes all rows from the suppliers table that do not match the criteria.

From the following SELECTtest, we can see supplierthat the table now only contains Downtownsuppliers listed as :

SELECT * FROM supplier

result:

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

MySQL using LIKE in case insensitive search

Publish Date:2025/04/21 Views:131 Category:MySQL

This tutorial article will show you how to use LIKE the operator to search a column. We will show you how to use it correctly to include all results that meet certain criteria, regardless of whether the value is uppercase or lowercase. LIKE

Loop PHP MySQLi Get Array Function

Publish Date:2025/04/21 Views:105 Category:MySQL

MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLi with queries. In this article, we will see mysqli_fetch_array() the use of functions and methods to iterate

Generate a random and unique string in MySQL

Publish Date:2025/04/21 Views:77 Category:MySQL

Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5() , RAND() , , SUBSTR() and UUID() . There is no inbuilt method to generate random string in MySQL, but there are many

Changing MySQL root password on Mac

Publish Date:2025/04/21 Views:160 Category:MySQL

This article teaches you how to change the MySQL user password on OS X. root We will be using XAMPP so that you can change the password using the MySQL console. Installing XAMPP for OSX First, download and install XAMPP for OSX from Apache

Using CURRENT_TIMESTAMP as a default value in MySQL

Publish Date:2025/04/21 Views:196 Category:MySQL

This article teaches you how to use as 5.6.5 in MySQL versions lower than . Thus, you can prevent MySQL error 1293. CURRENT_TIMESTAMP DEFAULT Our approach involves reordering table columns and using DEFAULT 0 and time values. Reproduce the

If ELSE in MySQL

Publish Date:2025/04/11 Views:86 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial