MySQL присоединиться к вопросу - PullRequest
0 голосов
/ 24 мая 2011
mysql> select * from new_table;
+-------------+
| idnew_table |
+-------------+
|           1 |
|           2 |
+-------------+

mysql> select * from second_table;
+----------------+--------+
| idsecond_table | second |
+----------------+--------+
|            100 |      1 |
|            150 |      1 |
|            200 |      2 |
+----------------+--------+

mysql> select * from third;
+---------+-------+
| idthird | third |
+---------+-------+
|     500 |     1 |
|     600 |     2 |
+---------+-------+

и мне нужно присоединить его к одному, вот так

+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
|   1 | 100 |      1 |  500 |          1 |
|   1 | 150 |      1 | null |       null |
|   2 | 200 |      2 |  600 |          2 |
+-----+-----+--------+------+------------+

Я пытаюсь этот запрос

select 
t.idnew_table as tid,
sec.idsecond_table as sid ,
sec.second as secsec,
th.idthird as thid,
th.third as thirdthird
from
new_table t
join second_table sec on sec.second = t.idnew_table
join third th on th.third = t.idnew_table

но повторяет строки из третьей таблицы, как это

+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
|   1 | 100 |      1 |  500 |          1 |
|   1 | 150 |      1 |  500 |          1 |
|   2 | 200 |      2 |  600 |          2 |
+-----+-----+--------+------+------------+

так что мне нужен твой совет

1 Ответ

0 голосов
/ 24 мая 2011

Исходя из ваших комментариев и предоставленных вами данных, я полагаю, что вам нужна FULL OUTER JOIN второй и третьей таблицы, основанная на числовых значениях и идентификаторах (second_table.second и third_table.third), а затем (обычно) INNER JOIN к первому столу.

К сожалению, в таблицах нет номеров строк, и MYSQL не предоставляет оконных функций. Поэтому это возможно, но с чем-то ужасным:

SELECT
    fulljoin.*
FROM new_table t
JOIN
    (
    SELECT
         COALESCE(second,third) AS idnew_table
       , idsecond_table AS sid
       , second AS secsec
       , idthird AS thid
       , third AS thirdthird
    FROM
    (
    SELECT
        @rn := if(@g = second, @rn+1, 1) AS rownumber
      , second
      , idsecond_table
      , @g := second
    FROM
        ( select @g:=null, @rn:=0 ) AS initvars
    CROSS JOIN
        second_table
    ORDER BY
        second
      , idsecond_table
    ) AS grp2
    LEFT JOIN
    (
    SELECT
        @rn := if(@g = third, @rn+1, 1) AS rownumber
      , third
      , idthird
      , @g := third
    FROM
        ( select @g:=null, @rn:=0 ) AS initvars
    CROSS JOIN
        third_table
    ORDER BY
        third
      , idthird
    ) AS grp3
    ON grp2.second = grp3.third
    AND grp2.rownumber = grp3.rownumber

    UNION ALL

    SELECT
         COALESCE(second,third) AS idnew_table
       , idsecond_table AS sid
       , second AS secsec
       , idthird AS thid
       , third AS thirdthird
    FROM
    (
    SELECT
        @rn := if(@g = second, @rn+1, 1) AS rownumber
      , second
      , idsecond_table
      , @g := second
    FROM
        ( select @g:=null, @rn:=0 ) AS initvars
    CROSS JOIN
        second_table
    ORDER BY
        second
      , idsecond_table
    ) AS grp2
    RIGHT JOIN
    (
    SELECT
        @rn := if(@g = third, @rn+1, 1) AS rownumber
      , third
      , idthird
      , @g := third
    FROM
        ( select @g:=null, @rn:=0 ) AS initvars
    CROSS JOIN
        third_table
    ORDER BY
        third
      , idthird
    ) AS grp3
    ON grp2.second = grp3.third
    AND grp2.rownumber = grp3.rownumber
    WHERE grp2.second IS NULL
    ) AS fulljoin
ON fulljoin.idnew_table = t.idnew_table
;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...