My problem is similiar to this question: https://stackoverflow.com/a/7517904
I have 4 tables, which I link with keys A and B:
A:
A B C
1 a c
B:
A B D
1 a d
1 a e
C:
A B E
1 a h
1 a i
D:
A B F
1 a k
1 a l
I want to join the tables together, so that the result should be:
A B C D E F
1 a c d h k
1 a c e i l
My actual result is like this:
A B C D E F
1 a c d h k
1 a c e h k
1 a c d h l
1 a c e h l
1 a c e i l
1 a c d i l
1 a c d i k
1 a c e i k
The problem is, that I get all combinations. I want to have as less rows as possible. I tried the solution of the other thread, using rownumbers:
SELECT*
FROM (SELECT @rowsnum := @rowsnum + 1 AS ae1num,
Concat(ae1.a, ae1.b) AS id,
ae1.*
FROM A as ae1,
(SELECT @rowsnum := 0) r
ORDER BY ae1.a,
ae1.b) ae1
LEFT JOIN (SELECT aenum,
Concat(ae111.a, ae111.b) AS id,
aecm.*
FROM (SELECT @rownum := @rownum + 1 AS aenum,
Concat(ae11.a, ae11.b) AS id,
ae11.*
FROM A as ae11,
(SELECT @rownum := 0) a
ORDER BY ae11.a,
ae11.b) ae111
LEFT JOIN B as aecm
ON ae111.a = aecm.a
AND ae111.b = aecm.b
ORDER BY ae111.a,
ae111.b) aec
ON ae1.a = aec.a
AND ae1.b = aec.b
AND aec.aenum = ae1.ae1num
I hope, I described my problem clear enough.