Skip to content

Instantly share code, notes, and snippets.

@jazio
Last active March 6, 2018 11:17
Show Gist options
  • Save jazio/d53dc5f05d5c875bfec100ece1c499f1 to your computer and use it in GitHub Desktop.
Save jazio/d53dc5f05d5c875bfec100ece1c499f1 to your computer and use it in GitHub Desktop.
Using mySQL subqueries for selective deleting
mysql> select entity_id, language from field_data_body where entity_id = 9311;
+-----------+----------+
| entity_id | language |
+-----------+----------+
| 9311 | en |
| 9311 | und |
+-----------+----------+
2 rows in set (0.00 sec)
# I need to delete only those entries with language 'und'
# See which nodes
mysql> select entity_id, language from field_data_body where language = 'und' AND entity_id IN (SELECT entity_id from field_data_body where language='en');
...
| 8798 | und |
| 9029 | und |
| 9030 | und |
| 9311 | und |
+-----------+----------+
156 rows in set (0.05 sec)
# Now, delete them using a subquery
# x is an alias for the temp table produced for the subquery.
mysql> DELETE from field_data_body WHERE language='und' AND entity_id IN (SELECT entity_id from (SELECT entity_id from field_data_body where language ='en') x);
Note: The following won't work:
DELETE from field_data_body WHERE language='und' AND entity_id IN (SELECT entity_id from field_data_body where language ='en);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment