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
- Both your tables had a id column that is their primary key
- 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.
-
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.