Home > Coding, MySQL > Delete from same table as select

Delete from same table as select

Simple code to delete from same table as when selecting from it. MySQL doesn’t allow you to delete from same table as you select.

So this is not possible:

DELETE FROM chess inner join tennis on chess.some_id = tennis.some_id where chess.some_col = ‘great’;

You will get an error saying: You can’t specify target table ‘chess’ for update in FROM clause.

So you need to create a temp table and then delete.

DELETE FROM chess where some_id in (select some_id from
(SELECT chess.some_id FROM chess inner join tennis on tennis.some_id = chess.some_id where some_col = ‘great’
) as y);

Done, easy.

Advertisements
Categories: Coding, MySQL
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: