Delete from table if identifier does not exist in another table

I want to remove an identifier from types that cannot be found in types_photos , but I don't know how to do this. id_type in types_photos matches the id in types . This is what the table structure looks like:

 CREATE TABLE IF NOT EXISTS `types` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_user_added` int(11) DEFAULT '0', `id_user_edited` int(11) DEFAULT '0', `data_name` text NOT NULL, `data_name_seo` text NOT NULL, `data_type` enum('tag','equipment','search') NOT NULL, `datetime_added` datetime NOT NULL, `datetime_edited` datetime NOT NULL, `ipaddress_added` text NOT NULL, `ipaddress_edited` text NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) CREATE TABLE IF NOT EXISTS `types_photos` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_user_added` int(11) DEFAULT '0', `id_user_edited` int(11) DEFAULT '0', `id_type` int(11) DEFAULT '0', `id_photo` int(11) DEFAULT '0', `datetime_added` datetime NOT NULL, `datetime_edited` datetime NOT NULL, `ipaddress_added` text NOT NULL, `ipaddress_edited` text NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) 

So my question is: how can I remove all id from types that cannot be found in types_photos ?

+7
sql sql-delete multiple-tables
source share
1 answer
 DELETE FROM types WHERE id NOT IN ( SELECT ID FROM types_photos ) 
+17
source share

All Articles