If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Difference between two tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-09, 15:10
vishnukumar_sun vishnukumar_sun is offline
Registered User
 
Join Date: Sep 2009
Posts: 1
Difference between two tables

Hi,

I have two tables in the database. The two tables are same schema. One act as source and other as destination. I have to find the difference between two tables. The difference is the records present in the source and not in destination. The table has composite key. I have used INNER JOIN, as shown below

SELECT DISTINCT source.locations, source.station, source.unit_details,
FROM production.step_instances source
INNER JOIN test.step_instances destination ON ( destination.locations!= source.locations AND destination.station != source.station AND destination.unit_details!= source.unit_details);

The columns locations, station and unit_details are composite keys with different data types. But I found, some issues with the data in the table from the above query. Is this possible to use some mysql function for composite key or other query to find the difference?

Thanks
Reply With Quote
  #2 (permalink)  
Old 09-29-09, 16:14
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
how about:
SELECT DISTINCT source.locations, source.station, source.unit_details,
FROM production.step_instances source
WHERE NOT EXISTS (select 1 from test.step_instances destination
WHERE destination.locations = source.locations AND destination.station = source.station AND destination.unit_details = source.unit_details)


Dave Nance
Reply With Quote
  #3 (permalink)  
Old 09-29-09, 17:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT source.locations
     , source.station
     , source.unit_details
  FROM production.step_instances AS source
LEFT OUTER
  JOIN test.step_instances AS destination 
    ON destination.locations = source.locations 
   AND destination.station = source.station 
   AND destination.unit_details = source.unit_details
 WHERE destination.locations IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-29-09, 22:57
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On