linein designs

01 Feb, 2007

Compare Two MySQL Tables

Posted by: john In: mysql

The other day I needed to compare two tables and see what data was in one but not in the other.

table_a is my main table that I want to update and table_b is my storage table that I have my updates in. The query below will return all records that are in table_b, but no in table_a.


SELECT table_b.* FROM table_b
LEFT JOIN table_a ON table_b.id = table_a.id
WHERE table_a.item_id IS NULL

The you can take those results and use them to INSERT the missing records.

This is the best way I can think of. If anyone has a better way, please leave a comment on how you would recommend.

Similar Posts:

6 Comments »

Comment by Palmetto

This is palmetto from digg.. thanks for the info about myphpadmin and mysql.

@palmetto – if you’ve already got a site and a company with PHP & MySQL hosting your site, hop on over to http://www.linein.org/blog/ and post a comment with your email address. I’d be more than happy to help. Both are really easy to setup and get started.

thanks for the help.. I will be learning now. ;)

 
Comment by Catalyst

Thanks for this little article, we have used this to compare commercial gas prices for our customers.

 
Comment by Andrew Q

This just saved my butt. A winner is you!

 
Comment by Itamar

hi,
If you want more options, you can also use a third party tool, like Nob Hill’s compare :
http://www.nobhillsoft.com/NHDBCompare.aspx

 
Comment by English

Thanks for that useful piece of code. It saved my time
A little smile in right upper part of the page is funny =)

 
Comment by JBES

SELECT * FROM tblA
WHERE tblA.ID
NOT IN (
SELECT tblB.ID
FROM tblB
WHERE tblA.ID=tblB.ID
)

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post



Please Support Our Friends

Send emails with ease on this SMTP server by JangoSmtp.com.

Banner

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.