Last active
March 6, 2018 11:17
-
-
Save jazio/d53dc5f05d5c875bfec100ece1c499f1 to your computer and use it in GitHub Desktop.
Using mySQL subqueries for selective deleting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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