Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-20-2005, 09:06 PM   PM User | #1
srarcade
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
srarcade is an unknown quantity at this point
Comparing 2 tables for differences

Hello- Trying to seperate data differences between 2 tables. Lets say i have 2 tables like the following

tbl_1
int_id - str_name
1 - Bob
2 - Roger
3 - Kim
4 - Jane

tbl_2
int_id - str_name
1 - Bob
2 - John
3 - Kim
4 - George

How could i construct a query to get results of the differences? Like seeing what names are in tbl_2 that are not in tbl_1? Or what is not in tbl_2 that is in tbl_1?

Any help appreciated! Thanks.
srarcade is offline   Reply With Quote
Old 01-20-2005, 09:13 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
Welcome here!

I'm not completely sure wha you are asking for.
Do you want to get the rows where the id-name combination is different? or just all names that don't occur in both tables?

so if
tbl_1
int_id - str_name
1 - Bob
2 - Roger
3 - Kim
4 - Jane

and

tbl_2
int_id - str_name
1 - Jane
2 - Roger
3 - Bob
4 - Jack

what shoukd then be the result?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-20-2005, 09:22 PM   PM User | #3
srarcade
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
srarcade is an unknown quantity at this point
ok im looking for just the names

so query1 would be 'select just names that appear in table 2' and it would produce the output of:
tbl_2
John
George

query2 would be the same thing, except with table 1 only names instead.
srarcade is offline   Reply With Quote
Old 01-20-2005, 09:39 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
sorry, but you're not realy clear in your explanation.

do you want all names that only occur in one of the tables?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-20-2005, 09:52 PM   PM User | #5
srarcade
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
srarcade is an unknown quantity at this point
ok like you said is correct, im trying to have my query result print just the names that only appear in table 2. Any names in table 1 that are also in table 2 would be ommitted. Ive done this before a very long time ago, i just cant remember how i did it.
srarcade is offline   Reply With Quote
Old 01-20-2005, 10:07 PM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
well, the easiest way is using a subquery:

select str_name from table2 where strname Not In (select str_name from table1)

but you can only use subselects in mysql v4.1 and up.
if you have an older version, then you need to use an outer join.

select table2.str_name FROM table2 LEFT JOIN table1 ON table2.str_name=table1.str_name WHERE table1.str_name IS NULL;

The str_name columns should then be defined as NOT NULL !

what version do you have?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-20-2005, 10:27 PM   PM User | #7
srarcade
New to the CF scene

 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
srarcade is an unknown quantity at this point
I have version 4.0.18 The JOIN query works great, gives me just what I needed. Thanks alot for you help and patience!
srarcade is offline   Reply With Quote
Old 01-20-2005, 10:43 PM   PM User | #8
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
you're welcome glad i could help.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:30 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.