-1

My example:

Table A: post_id

Table B: id + delete(null)

  • In that, post_id = id

I want to delete all rows for both tables where A.deleted != 'null'

I tried: Delete A, B FROM A LEFT JOIN B ON B.post_id = A.id WHERE A.deleted != 'null' - but not work

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tanker
  • 33
  • 5
  • Check this article https://www.mysqltutorial.org/mysql-delete-join/ – Ravi Makwana Feb 15 '20 at 09:57
  • is it showing syntax error? What is relation between `A` and `B`. can you share your data – Ravi Makwana Feb 15 '20 at 09:59
  • I suggest first make working select query than make it delete query – Ravi Makwana Feb 15 '20 at 10:00
  • @RaviMakwana I checked and try all example, it delete all my rows. A & B have same id as I mention: post_id from A, id from B sir – Tanker Feb 15 '20 at 10:02
  • I mean is it **one to one** or **one to many** relation in that case you can use `LEFT JOIN` instead of `JOIN` – Ravi Makwana Feb 15 '20 at 10:07
  • @RaviMakwana sorry, I write mistake, it should be A.deleted != 'null'. So when I run: **Delete A, B FROM A LEFT JOIN B ON B.post_id = A.id WHERE A.deleted != 'null'** - it remove nearly all my row – Tanker Feb 15 '20 at 10:12
  • oh man, that's why mostly suggest to first create Select Query – Ravi Makwana Feb 15 '20 at 10:13
  • 3
    Does this answer your question? [Mysql - delete from multiple tables with one query](https://stackoverflow.com/questions/4839905/mysql-delete-from-multiple-tables-with-one-query) – Ravi Makwana Feb 15 '20 at 10:20

1 Answers1

0

If your no record fetch for delete != 'null' then you should add deleted is not null OR delete != 'null'

You can not check null value with == or != operator you need to use is or is not for this kind of filtration

Delete A, B 
    FROM A LEFT JOIN B ON B.post_id = A.id 
    WHERE  A.deleted is not null OR A.deleted != 'null' 
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41