Home >Database >Mysql Tutorial >How to Delete Rows Using LEFT JOIN in MySQL DELETE Queries?

How to Delete Rows Using LEFT JOIN in MySQL DELETE Queries?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-30 09:07:15503browse

How to Delete Rows Using LEFT JOIN in MySQL DELETE Queries?

Deleting Deadlines Using LEFT JOIN in MySQL

In the context of managing job deadlines and job descriptions stored in two tables, you have encountered difficulties in deleting rows based on a LEFT JOIN query. To resolve this, it's crucial to understand the correct syntax for executing DELETE queries with LEFT JOINs.

The LEFT JOIN statement enables you to select rows from the "left" table (in this case, deadline) and include matching rows from the "right" table (in this case, job) based on a join condition. However, deleting rows using LEFT JOIN requires a subtle change in your query syntax.

To successfully delete rows from the deadline table based on a LEFT JOIN with the job table, you need to explicitly specify the table from which the rows will be deleted. Here are the possible variations:

Delete Only Deadline Rows

If you wish to delete only the deadline rows, your query should be modified as follows:

DELETE `deadline` FROM `deadline`
LEFT JOIN `job`
ON deadline.job_id = job.job_id
WHERE `status` = 'szamlazva'
OR `status` = 'szamlazhato'
OR `status` = 'fizetve'
OR `status` = 'szallitva'
OR `status` = 'storno';

In this query, DELETE deadline FROM specifies that you want to delete rows from the deadline table. The LEFT JOIN is used to filter the rows based on the job table's status column.

Delete Deadline and Job Rows

If you want to delete both the deadline and job rows based on the LEFT JOIN criteria, you should use the following query:

DELETE `deadline`, `job` FROM `deadline`
LEFT JOIN `job`
ON deadline.job_id = job.job_id
WHERE `status` = 'szamlazva'
OR `status` = 'szamlazhato'
OR `status` = 'fizetve'
OR `status` = 'szallitva'
OR `status` = 'storno';

Here, DELETE deadline, job FROM specifies that you want to delete rows from both the deadline and job tables.

Delete Only Job Rows

If you want to delete only the job rows, but keep the deadline rows, you should use the following query:

DELETE `job` FROM `deadline`
LEFT JOIN `job`
ON deadline.job_id = job.job_id
WHERE `status` = 'szamlazva'
OR `status` = 'szamlazhato'
OR `status` = 'fizetve'
OR `status` = 'szallitva'
OR `status` = 'storno';

It's important to keep in mind that the order of table names in the DELETE statement can impact which rows will be deleted.

The above is the detailed content of How to Delete Rows Using LEFT JOIN in MySQL DELETE Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn