Deleting by Conditions Using Multiple Tables in MySQL

Asked 2 years ago, Updated 2 years ago, 42 views

I would like to extract duplicate data from two tables in MySQL and delete duplicate data.

This is what I'm thinking.Suppose you have a table like this.
Table A
|Address|Date of birth|Name|
| XX City | January 1st | Taro |
|YY City | January 2nd | Hanako |
| XX City | January 3rd | Ichiro |
|YY City | January 4th | Saburo |

Table B
|Address|Date of birth|Name|
| XX City | January 1st | Taro |
|YY City | February 2nd | Hanako |
| XX City | March 3rd | Jiro |
|YY City | January 4th | Saburo |

How do I compare tables A and B and delete only records from table B that match A and B's address, date of birth, and name?
I couldn't think of a SQL statement well, so I would appreciate it if you could lend me your wisdom.
It's actually a lot more data.
The result is
Table B
|Address|Date of birth|Name|
|YY City | February 2nd | Hanako |
| XX City | March 3rd | Jiro |

I would like to make it

mysql sql

2022-09-30 17:58

1 Answers

You can do it with one of the following queries:

 DELETE FROM BUSINESSING A JOIN BUSINESSING;
DELETE FROM B USING A NATURAL JOIN B;
DELETE B FROM A JOIN BUSING;
DELETE B FROM A NATURAL JOIN B;

Let's look at the manual for more information on syntax.
https://dev.mysql.com/doc/refman/5.6/ja/delete.html


2022-09-30 17:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.