peppericious Posted April 13, 2012 Share Posted April 13, 2012 Supposing I have 2 tables, A and B. For every record in A, there's supposed to be a corresponding one in B. However, there are a few records in A which *don't* have a corresponding record in B. I want to flag these records in A. A is like this: id | some_col flag ================================= 1 | S71D431W 0 2 | P79D431V 0 3 | S79R431Y 0 4 | Y085HG5D 1 (a corresponding record should exist in B with this value, but doesn't, so flag it) 5 | A79E433V 0 B is like this: f_some_col | another_col =========================== S71D431W | .. P79D431V | .. S79R431Y | .. A79E433V | .. Can anyone tell me how to loop through the records in A, flagging records which do not have a corresponding record in B? Thx in advance... I don't know whether this is an extremely simple thing... the solution is eluding me, however... Quote Link to comment https://forums.phpfreaks.com/topic/260858-flagging-record-in-table-a-if-it-does-not-have-a-corresponding-record-in-table-b/ Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 Something like this should do it : UPDATE table_a SET flag = 1 where table_a.some_field NOT IN (SELECT some_other_field FROM table_b) Quote Link to comment https://forums.phpfreaks.com/topic/260858-flagging-record-in-table-a-if-it-does-not-have-a-corresponding-record-in-table-b/#findComment-1336984 Share on other sites More sharing options...
peppericious Posted April 13, 2012 Author Share Posted April 13, 2012 Something like this should do it : UPDATE table_a SET flag = 1 where table_a.some_field NOT IN (SELECT some_other_field FROM table_b) Works beautifully! Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/260858-flagging-record-in-table-a-if-it-does-not-have-a-corresponding-record-in-table-b/#findComment-1336990 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.