0

I'm getting this error when running the code:

Error Code: 1093. You can't specify target table 'details' for update in FROM clause

DELETE FROM details WHERE detail NOT IN 
(
    SELECT detail 
    FROM user_details
    JOIN data ON user_details.data_iddata = data.iddata
    JOIN details ON details.iddetails = data.details_iddetails
)

What am I doing wrong here?

Ethan Allen
  • 13,099
  • 22
  • 89
  • 175

2 Answers2

0

I don't have a system handy, but I think the problem is that you can reference the same table in a subquery as the main one you are deleting from. I.e. it doesn't like JOIN details ON details.iddetails = data.details_iddetails in the subquery because that's in the main select clause.

One workaround is to create a temp table, insert the records you are interested in into it, and then find your set from a join off of that.

user151841
  • 15,348
  • 28
  • 93
  • 156
0

You're trying to do a DELETE from a table while you're doing a SELECT from it.

In order to get this to work, make a list from your SELECT query and put its results in the parentheses.

If you aren't doing this in an external script, you should define a view that will give you the desired SELECT output.

marklark
  • 808
  • 1
  • 8
  • 18