kjtocool Posted January 2, 2008 Share Posted January 2, 2008 I have two tables: weeks and actuals. I need to write a query which selects all rows from the weeks table that aren't in the actuals table. Both tables share a week_id which should be used for the comparison. So I need something like: SELECT * FROM weeks WHERE weeks.week_id doesn't exist in actuals ... Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/ Share on other sites More sharing options...
remyaarun Posted January 2, 2008 Share Posted January 2, 2008 select * from weeks left join actuals on weeks.id = actuals.id; Try out with this code. Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428545 Share on other sites More sharing options...
kjtocool Posted January 2, 2008 Author Share Posted January 2, 2008 I tried: SELECT * FROM bonanza_weeks LEFT JOIN bonanza_actuals ON bonanza_weeks.week_id = bonanza_actuals.week_id It doesn't work properly, it just returns every row in bonanza_weeks. In each table, week_id is a unique value, only appearing once per table. Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428553 Share on other sites More sharing options...
remyaarun Posted January 2, 2008 Share Posted January 2, 2008 select * from weeks where weeks.week_id not in (select * from actuals where actuals.id=weeks.week_id) try with this Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428564 Share on other sites More sharing options...
kjtocool Posted January 2, 2008 Author Share Posted January 2, 2008 SELECT * FROM bonanza_weeks WHERE bonanza_weeks.week_id NOT IN ( SELECT * FROM bonanza_actuals WHERE bonanza_actuals.week_id = bonanza_weeks.week_ID ) Returns Error: #1241 - Operand should contain 1 column(s) Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428595 Share on other sites More sharing options...
remyaarun Posted January 2, 2008 Share Posted January 2, 2008 SELECT * FROM bonanza_weeks WHERE bonanza_weeks.week_id NOT IN (SELECT bonanza_actuals.week_id FROM bonanza_actuals WHERE bonanza_actuals.week_id = bonanza_weeks.week_ID) Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428609 Share on other sites More sharing options...
kjtocool Posted January 2, 2008 Author Share Posted January 2, 2008 SELECT * FROM bonanza_weeks WHERE bonanza_weeks.week_id NOT IN ( SELECT bonanza_actuals.week_id FROM bonanza_actuals WHERE bonanza_actuals.week_id = bonanza_weeks.week_ID ) Works perfectly. Thanks a bunch! Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428622 Share on other sites More sharing options...
fenway Posted January 2, 2008 Share Posted January 2, 2008 That can be very slow, since it's a correlated subquery -- it has to run for EVERY record in the outer table... a LEFT JOIN ... IS NULL would be better. Link to comment https://forums.phpfreaks.com/topic/84179-solved-help-with-two-table-query/#findComment-428750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.