クイック アップデート -- SQLFiddle リンク: http://sqlfiddle.com/#!2/d038f/2
比較的わかりやすいと思いますが…
全部で 7 つのテーブル、3 つの「メイン」テーブル、「多対多」関係を管理する別の 3 つ、マスター テーブルである別の 1 つがあります。
mysql> show tables;
+--------------------+
| Tables_in_test |
+--------------------+
| Equipment |
| Room |
| Trainer |
| Training |
| training_equipment |
| training_room |
| training_trainer |
+--------------------+
7 rows in set (0.00 sec)
さて、スキーマと内容:
mysql> SELECT * FROM Equipment;
SELECT * FROM Room;
SELECT * FROM Trainer;
SELECT * FROM training_equipment;
SELECT * FROM training_room;
SELECT * FROM training_trainer;
SELECT * FROM Training;
+----+-------------+
| id | equipment |
+----+-------------+
| 1 | Equipment_1 |
| 2 | Equipment_2 |
| 3 | Equipment_3 |
| 4 | Equipment_4 |
+----+-------------+
4 rows in set (0.00 sec)
+----+--------+
| id | room |
+----+--------+
| 1 | Room_1 |
| 2 | Room_2 |
+----+--------+
2 rows in set (0.00 sec)
+----+-------+
| id | name |
+----+-------+
| 1 | John |
| 2 | Joe |
| 3 | Jason |
+----+-------+
3 rows in set (0.00 sec)
+-------------+--------------+
| training_id | equipment_id |
+-------------+--------------+
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
+-------------+--------------+
3 rows in set (0.00 sec)
+-------------+---------+
| training_id | room_id |
+-------------+---------+
| 1 | 1 |
+-------------+---------+
1 row in set (0.01 sec)
+-------------+------------+
| training_id | trainer_id |
+-------------+------------+
| 1 | 2 |
| 1 | 3 |
+-------------+------------+
2 rows in set (0.00 sec)
+----+------------+------------+---------+------+-----------+---+
| id | from | to | trainer | room | equipment | a |
+----+------------+------------+---------+------+-----------+---+
| 1 | 1349578297 | 1350096689 | 1 | 1 | 1 | 0 |
+----+------------+------------+---------+------+-----------+---+
1 row in set (0.02 sec)
次のクエリを思いつきましたが、結果が正しくないことがわかります。
mysql> SELECT t.from, r.room, tra.name, e.equipment
-> FROM Training t
-> LEFT JOIN training_room tr ON ( t.room = tr.training_id )
-> LEFT JOIN Room r ON ( tr.room_id = r.id )
-> LEFT JOIN training_trainer tt ON ( t.trainer = tt.training_id )
-> LEFT JOIN Trainer tra ON ( tt.trainer_id = tra.id )
-> LEFT JOIN training_equipment te ON ( t.equipment = te.training_id)
-> LEFT JOIN Equipment e ON ( te.equipment_id = e.id )
-> WHERE t.id =1;
+------------+--------+------+-------------+
| from | room | name | equipment |
+------------+--------+------+-------------+
| 1349578297 | Room_1 | Joe | Equipment_3 |
| 1349578297 | Room_1 | Joe | Equipment_4 |
| 1349578297 | Room_1 | Jason| Equipment_3 |
| 1349578297 | Room_1 | Jason| Equipment_4 |
+------------+--------+------+-------------+
4 rows in set (0.02 sec)
重複した結果を見たくありません。見たいのは次のとおりです。
+------------+--------+------+-------------+
| from | room | name | equipment |
+------------+--------+------+-------------+
| 1349578297 | Room_1 | Joe | Equipment_3 |
| 1349578297 | Room_1 | Jason| Equipment_4 |
+------------+--------+------+-------------+
DISTINCT
問題を解決しないGROUP BY tra.name, e.equipment
ありがとうございました。