S
S
Sazanovdm2021-05-01 23:49:04
MySQL
Sazanovdm, 2021-05-01 23:49:04

How to delete a row from 2 tables in one query?

I need to delete rows with Id = 80 in 2 tables, tell me what I'm doing wrong?

DELETE FROM table_1 A LEFT JOIN table_2 B ON (A.Id= B.Id) WHERE Id= '80' AND A.Type= '0'

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Immortal_pony, 2021-05-02
@Sazanovdm

DELETE 
    A,
    B
FROM 
    table_1 A
    LEFT JOIN table_2 B ON (A.Id= B.Id)
WHERE 
    A.Id=80

H
HemulGM, 2021-05-01
@HemulGM

DELETE FROM table_1 WHERE ...; DELETE FROM table_2 WHERE ...;

D
Dmitry Sviridov, 2021-05-02
@dimuska139

Hang up foreign key with property ON DELETE... CASCADE on the necessary column. Then the DBMS will automatically remove related data from other tables.

I
idShura, 2021-05-02
@idShura

BEGIN;
  DELETE FROM table_1 WHERE ID = 80; 
  DELETE FROM table_2 WHERE ID = 80;
COMMIT;

A
Akina, 2021-05-02
@Akina

what am I doing wrong?

Believe that a record with the specified ID exists.
If there is no such record in table_1 , there will be no deletions, even if there are such records in the second table.
In general, a FULL JOIN is required to solve a problem with a single query. But alas, MySQL does not support it. Therefore, in the general case, the problem is unsolvable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question