suttercain Posted September 15, 2009 Share Posted September 15, 2009 Hi everyone, I can't seem to wrap my head around a need of a current project. Let's save I have two tables, Table A and Table B, and all of the columns are identical. What I need to do is compare these two tables and find out if any of the Records were added or deleted since Table A, in table B. TABLE A ---------- | column | ---------- | 0001 | ---------- | 0002 | ---------- | 0003 | ---------- | 0004 | ---------- TABLE B ---------- | column | ---------- | 0001 | ---------- | 0003 | ---------- | 0005 | ---------- So the report I would need to see, after comparing TABLE A against TABLE B is that, Records 0002 and 0004 have been dropped, and Record 0005 has been added. Does anyone know the best way to approach this? Thanks. -SC Quote Link to comment https://forums.phpfreaks.com/topic/174340-solved-table-comparison-for-record-added-or-removed/ Share on other sites More sharing options...
artacus Posted September 15, 2009 Share Posted September 15, 2009 I don't use mysql enough to tell you if it does full outer joins. I don't believe it does. But you can do it like so. SELECT id, 'inserted' AS action FROM tablea WHERE id NOT IN (SELECT id FROM tableb) UNION ALL SELECT id, 'deleted' FROM tableb WHERE id NOT IN (SELECT id FROM tablea) Quote Link to comment https://forums.phpfreaks.com/topic/174340-solved-table-comparison-for-record-added-or-removed/#findComment-919220 Share on other sites More sharing options...
suttercain Posted September 17, 2009 Author Share Posted September 17, 2009 Artacus, So far so good. I ran the query you provided and it echos 2 4 5. This is good because 2 and 4 were the numbers deleted and 5 was the number added. My question would now be this, is there away to echo that 2 and 4 were the numbers deleted and that 5 was the number added? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/174340-solved-table-comparison-for-record-added-or-removed/#findComment-920135 Share on other sites More sharing options...
suttercain Posted September 17, 2009 Author Share Posted September 17, 2009 D'oh! Got it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/174340-solved-table-comparison-for-record-added-or-removed/#findComment-920174 Share on other sites More sharing options...
artacus Posted September 17, 2009 Share Posted September 17, 2009 Didn't expect me to be a step ahead of you huh? Quote Link to comment https://forums.phpfreaks.com/topic/174340-solved-table-comparison-for-record-added-or-removed/#findComment-920223 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.