Jump to content


Checking Two Tables For Matching Records

  • Please log in to reply
1 reply to this topic

#1 c_pattle

  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 December 2012 - 03:55 PM

I was just wondering if anyone knows what the most efficient way of checking to see if two tables contain matching records.

So for example I have a table called 'orders' which have a primary key of order_id.

Then I have a table called 'order_comments' which also has a field called order_id and I want to run a query that will check which orders don't have entries in order_comments.

Thanks for any help

#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 17,839 posts

Posted 13 December 2012 - 05:28 PM

SELECT o.order_id
FROM orders o
LEFT JOIN order_comments c USING (order_id)
WHERE c.order_id IS NULL

Edited by Barand, 13 December 2012 - 05:28 PM.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users