0

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.

  • See: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) - you can probably simplify the example. – Strawberry Dec 12 '17 at 14:42

1 Answers1

0

What you're saying, I think is that you want A joined to the first row of B, the first row of C and the first row of D, and A joined to the second row of B, the second row of C and the second row of D

Relational databases do not have any implied sequence in the rows; if you want to have a sequence it must be a column value. There are good reasons for this, but too long to explain here. The result is that you need a column for sequence, like this:

create table a (a integer, b varchar(1), c varchar(1));
insert into a (a, b, c) values (1, 'a', 'c');

create table b (a integer, b varchar(1), sequence integer, d varchar(1));
insert into b (a, b, sequence, d) values (1, 'a', 1, 'd');
insert into b (a, b, sequence, d) values (1, 'a', 2, 'e');

create table c (a integer, b varchar(1), sequence integer, e varchar(1));
insert into c (a, b, sequence, e) values (1, 'a', 1, 'h');
insert into c (a, b, sequence, e) values (1, 'a', 2, 'i');

create table d (a integer, b varchar(1), sequence integer, f varchar(1));
insert into d (a, b, sequence, f) values (1, 'a', 1, 'k');
insert into d (a, b, sequence, f) values (1, 'a', 2, 'l');

Then the query is:

select distinct
    a.a,
    a.b,
    a.c,
    b.d,
    c.e,
    d.f
from 
    a 
    join 
    b on a.a = b.a and a.b = b.b 
    join 
    c on a.a = c.a and a.b = c.b and c.sequence = b.sequence
    join 
    d on a.a = d.a and a.b = d.b and d.sequence = b.sequence;

And the result is:

+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
| 1 | a | c | d | h | k |
| 1 | a | c | e | i | l |
+---+---+---+---+---+---+
2 rows in set (0.00 sec)
Ron Ballard
  • 693
  • 6
  • 8