dlebowski Posted June 5, 2007 Share Posted June 5, 2007 For some reason, I am struggling with the query, maybe someone can help me out. Here is what I'm trying to do: Before Query: Table 1 Table 2 ID | DATE ID | DATE ------------- ----------- 1 2007-01-01 1 2007-01-01 2 2007-01-01 1 2007-01-01 3 2007-01-01 2 2007-01-01 4 2007-12-12 3 2007-01-01 5 2007-01-01 3 2007-01-01 4 2007-12-12 After Query: Table 1 Table 2 ID | DATE ID | DATE ------------- ----------- 1 2007-01-01 1 2007-01-01 2 2007-01-01 1 2007-01-01 3 2007-01-01 2 2007-01-01 4 2007-12-12 3 2007-01-01 3 2007-01-01 4 2007-12-12 I need the query to take the values in Table 1 and in Table 2 and compare them. If Table 1 has an ID that doesn't exist anywhere Table 2 with the same date, then Table 1 needs to have that row removed. Any help would be greatly appreciated. Please let me know if I need to elaborate. Ryan Quote Link to comment https://forums.phpfreaks.com/topic/54336-solved-compare-two-tables-delete-non-matching-values-mysql/ Share on other sites More sharing options...
pikemsu28 Posted June 5, 2007 Share Posted June 5, 2007 $query = "delete from table1 where id not in (select id from table2)"; Quote Link to comment https://forums.phpfreaks.com/topic/54336-solved-compare-two-tables-delete-non-matching-values-mysql/#findComment-268745 Share on other sites More sharing options...
dlebowski Posted June 5, 2007 Author Share Posted June 5, 2007 Thank man. Exactly what I needed. I took it one step further to resolve my problem: DELETE * from table1 where id not in (select id from table2 WHERE Date = '$date) AND Date = '$date' Quote Link to comment https://forums.phpfreaks.com/topic/54336-solved-compare-two-tables-delete-non-matching-values-mysql/#findComment-268748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.