1 Replies - 546 Views - Last Post: 21 May 2018 - 07:19 PM Rate Topic: -----

#1 seniorCSGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 02-March 18

Deleting Multiple rows from multiple tables in Mysql

Posted 21 May 2018 - 06:22 PM

I have two tables Table1 and Table 2

Table1: columns : Id, LastUpdatedStamp(datatype : datetime)
Table2: columns : userId(foreignKey, same as Table1.Id)


Now, I need to delete rows from both tables...

my Where clause has to be in Table1 which is if the LastUpdatedStamp <= "2017-4-3 00:00:00", and get the Id for these rows

and delete the rows in Table2 which have that Id

but the cascade in on Table 2, so how do I do this, as my where clause is coming from Table1 but delete needs to first happen in Table 2.

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting Multiple rows from multiple tables in Mysql

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5465
  • View blog
  • Posts: 14,418
  • Joined: 18-April 07

Re: Deleting Multiple rows from multiple tables in Mysql

Posted 21 May 2018 - 07:19 PM

If this table is innodb, then you can setup a "cascade delete". Check out the terms "mysql ON DELETE CASCADE" on Google when setting up your constraints. The idea is that you setup a constraint on Table1 for the ID that will also cascade delete into Table2.

Here is a site that can help explain as well...

http://www.mysqltuto...delete-cascade/

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1