Finding duplicate values in 2 different tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhonda6373
    New Member
    • Mar 2010
    • 35

    #1

    Finding duplicate values in 2 different tables

    Hello,

    I am a very new beginner. I have the following problem I would like to solve. I have a column named matterno in a table named page0. I have another column named court in a table named page2. I would like to find all matterno that have the court duplicated in page2. I would like to include both matterno and court in the results. Can someone assist?

    Thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I'm not a SQL expert but I think the way to do this is to effectively join the table to itself. You have not really given enough detail about you table structures to be able to properly answer this, in particular you have missed out the data about the primary and foreign key fields defined in your tables.

    So to answer you I am going to assume that
    1. Both your tables had a id column that is their primary key
    2. Your page2 table has a page0Id column that is the foreign linking page2 to page0

    If these assumptions are wrong you will have to correct for them.

    Code:
    SELECT po.id, p0.matterno, p2a.id, p2a.court, p2a.id, p2b.court 
      FROM page0 AS p0 
      INNER JOIN page2 AS p2a ON p0.id = p2a.page0Id
      INNER JOIN page2 AS p2b ON p2a.court = p2b.court AND p2a.id != p2b.id;
    I suspect that this will output each duplicate twice but it rather depends on what you are using this for as to the amount of effort you need to go into to fix that.

    Comment

    Working...