0

I have 2 delete SQL query i want to make it one.

$sql1= DELETE FROM user WHERE userid={$id}

another is

$sql = Delete FROM user_meta where userid = {$id}

Any suggestions? I want to make it simple.

Anatolii Gabuza
  • 6,064
  • 2
  • 35
  • 53
user3201500
  • 1,417
  • 3
  • 21
  • 40

2 Answers2

0

You can't delete from 2 tables in DELETE statement, but you issue 2 statements in 1 go:

$sql = "DELETE FROM user WHERE userid={$id};
DELETE FROM user_meta WHERE meta_key = {$id};"
user1918305
  • 1,911
  • 1
  • 12
  • 14
0

The normal way of deleting things from different tables "at the same time" is to wrap them in a transaction. Something like:

begin transaction thedeletes
    DELETE FROM user WHERE userid={$id};
    DELETE FROM user_meta WHERE userid = {$id}
commit transaction thedeletes;

In MySQL you can actually put these in the same query:

delete u, um
    from user u join
         user_meta um
         on u.userid={$id} and um.userid = {$id};
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709