By not overlapping matches, I mean that each row should be returned at most once. That seems to be the hard part.
I managed to get the best not overlapping matches, using the following query where pair is a view that has all possible matches as (id1,id2,val1,val2) rows.
SELECT p.* FROM pair p
LEFT JOIN pair p1 ON p1.id1 = p.id1 AND p1.val2 < p.val2
LEFT JOIN pair p2 ON p2.id2 = p.id2 AND p2.val1 < p.val1
WHERE
p1.id1 IS NULL
AND p2.id2 IS NULL;
Full sqlfiddle here http://sqlfiddle.com/#!9/68614/2
For values a,b in t1 and a,d in t2 I want it to return pairs (a,a) and (b,d) but it only returns (a,a)
Could someone provide a working solution? Or if this kind of matching would fundamentally be better done on the client, could you explain why?
-- EDIT
The problem I'm trying to solve is similar to the one discussed here: Retrieving the last record in each group
My requirements are higher, I need in addition that matches don't overlap.