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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 02:38 AM   PM User | #1
Mike1963
New Coder

 
Join Date: May 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Mike1963 is an unknown quantity at this point
compare 2 db tables

hi everyone, i am trying to compare 2 tables and return the difference, i can join the tables (no problem there), but can`t get the difference in the 2. to clarify my problem, table1 has company_id as does table2 but table 2 does not have ALL the company_id`s as table 1 has, now i am wanting to find out the company_id`s that do not match. this is what i am currently trying, i am not getting any errors, all i am getting is the company_id`s that DO match.

Code:
$query=mysql_query("SELECT * from company as c1
INNER JOIN 
messages as c2
on c1.company_id=c2.company_id where c1.company_id != ''") or die (mysql_error());
Mike1963 is offline   Reply With Quote
Old 12-12-2012, 04:15 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
So what is it you *DO* want to get back?

EXPLICITLY!

Just the IDs there are in company but not in messages? Or the IDs and other fields?

Or do you want (EXAMPLE onyl!) thie IDs, the company name, and the count of matching messages?

OR OR OR...???

You need to spell out EXACTLY which fields you want from which table under what circumstance.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-12-2012, 04:34 AM   PM User | #3
Mike1963
New Coder

 
Join Date: May 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Mike1963 is an unknown quantity at this point
all i`m wanting is the company_id`s in the company table that dont match the messages table
Mike1963 is offline   Reply With Quote
Old 12-12-2012, 08:11 AM   PM User | #4
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
if you ment:

Code:
c1.id      c2.id
1             1
2
To find 2 as result:

Code:
SELECT c1.company_id 
from company as c1
LEFT JOIN  messages as c2
on c1.company_id=c2.company_id 
where c2.company_id is null
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 12-12-2012 at 08:16 AM..
BubikolRamios is offline   Reply With Quote
Old 12-12-2012, 03:31 PM   PM User | #5
Mike1963
New Coder

 
Join Date: May 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Mike1963 is an unknown quantity at this point
when i use c1.company_id or the * i get either ALL the id`s from the company table or ALL the id`s that do match. this is using either the LEFT JOIN or RIGHT JOIN, doesnt matter which one i use. i have tried ever way using left and right join and nothing is giving the right results
Mike1963 is offline   Reply With Quote
Old 12-12-2012, 03:37 PM   PM User | #6
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Did you try this:
Code:
where c2.company_id is null
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 12-12-2012, 03:55 PM   PM User | #7
Mike1963
New Coder

 
Join Date: May 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Mike1963 is an unknown quantity at this point
when i tried that earlier it wouldnt give any results but just tried it again and now it works!! ty so much, now to figure out how to get the company_name to show up in a drop down. lol always something, eh?
Mike1963 is offline   Reply With Quote
Old 12-12-2012, 04:40 PM   PM User | #8
Mike1963
New Coder

 
Join Date: May 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Mike1963 is an unknown quantity at this point
got it all figured out including the company_name in the drop down box. TY so very much for your help!
Mike1963 is offline   Reply With Quote
Old 01-01-2013, 08:59 AM   PM User | #9
rajdeep01
New Coder

 
Join Date: Dec 2012
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
rajdeep01 can only hope to improve
We use AdeptSQL's SQLDiff tool for the database structures, and it's not overly expensive. They also do a tool to compare data within tables.
rajdeep01 is offline   Reply With Quote
Old 01-02-2013, 02:31 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by rajdeep01 View Post
We use AdeptSQL's SQLDiff tool for the database structures, and it's not overly expensive. They also do a tool to compare data within tables.
This post has NOTHING TO DO with the subject of this thread.

If you keep posting IRRELEVANT comments like this, you are going to get some bad reputation marks.

If you are going to answer in a thread, READ the ENTIRE thread, first.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant 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 07:42 PM.


Advertisement
Log in to turn off these ads.