bucko Posted February 29, 2008 Share Posted February 29, 2008 Hi, I have this situation in mySQL 'table1' has an 'id' field. 'table2' has a 'table1_id' field, same value as table1:id I have deleted some entries from 'table1' Now I want to delete all entries in 'table2' where there is no 'table_id' in 'table1:id' Can someone please tell me nice way to perform that request? Thanks in advance for any suggestions. bucko. Link to comment https://forums.phpfreaks.com/topic/93700-delete-from-table-where-id-not-in-other-table/ Share on other sites More sharing options...
fri3ndly Posted February 29, 2008 Share Posted February 29, 2008 Hmm would it be something like DELETE FROM table2 WHERE table_id != 'id' Haven't tested it and dont know if that is valid SQL...give it a try though Link to comment https://forums.phpfreaks.com/topic/93700-delete-from-table-where-id-not-in-other-table/#findComment-480106 Share on other sites More sharing options...
drewbee Posted February 29, 2008 Share Posted February 29, 2008 This is psudo code, please backup your database before testing. DELETE FROM table2 WHERE table1_id NOT IN (SELECT id FROM table1) Note: REVERSE LOOKUPS (NOT IN) take a long time to execute if you have a lot of rows. Link to comment https://forums.phpfreaks.com/topic/93700-delete-from-table-where-id-not-in-other-table/#findComment-480114 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.