+-----+-----+-----+
| id | id1 | id2 |
+-----+-----+-----+
| 1 | NULL| 2 |
| 2 | 1 | NULL|
| 3 | 1 | NULL|
| 4 | NULL| 1 |
| 5 | 2 | NULL|
| 6 | 3 | NULL|
| 7 | NULL| 3 |
| 8 | 3 | NULL|
+-----+-----+-----+
MySQL и 3 таблицы
table1:
Код:
table2:
Код:
+-----+---------+
| id1 | name1 |
+-----+---------+
| 1 | value1 |
| 2 | value2 |
| 3 | value3 |
+-----+-----+--+
| id1 | name1 |
+-----+---------+
| 1 | value1 |
| 2 | value2 |
| 3 | value3 |
+-----+-----+--+
table3:
Код:
+-----+---------+
| id2 | name2 |
+-----+---------+
| 1 | val1 |
| 2 | val2 |
| 3 | val3 |
+-----+-----+--+
| id2 | name2 |
+-----+---------+
| 1 | val1 |
| 2 | val2 |
| 3 | val3 |
+-----+-----+--+
Необходимо сделать такой запрос чтобы:
Код:
+-----+--------+---------+
| id | name1 | name2 |
+-----+--------+---------+
| 1 | NULL | val2 |
| 2 | value1 | NULL |
| 3 | value1 | NULL |
| 4 | NULL | val1 |
| 5 | value2 | NULL |
| 6 | value3 | NULL |
| 7 | NULL | val3 |
| 8 | value3 | NULL |
+-----+--------+---------+
| id | name1 | name2 |
+-----+--------+---------+
| 1 | NULL | val2 |
| 2 | value1 | NULL |
| 3 | value1 | NULL |
| 4 | NULL | val1 |
| 5 | value2 | NULL |
| 6 | value3 | NULL |
| 7 | NULL | val3 |
| 8 | value3 | NULL |
+-----+--------+---------+
LEFT JOIN table2 AS t2 ON t1.id1 = t2.id1
LEFT JOIN table3 AS t3 ON t1.id2 = t3.id2
Примерно так.
щас разбираюсь как и почему.