4

Quick update -- SQLFiddle link: http://sqlfiddle.com/#!2/d038f/2

I think it's a relatively straight forward one...

I have 7 tables altogether, 3 'main' tables, another 3 that manages the 'many-to-many' relationships and another one which is the master table, more precisely:

mysql> show tables;
+--------------------+
| Tables_in_test     |
+--------------------+
| Equipment          |
| Room               |
| Trainer            |
| Training           |
| training_equipment |
| training_room      |
| training_trainer   |
+--------------------+
7 rows in set (0.00 sec)

Now, the schemas and contents:

    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)

I came up with the following query and you can see that the result is not correct:

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)

I don't want to see duplicate results, all I'd like to see would be:

+------------+--------+------+-------------+
| from       | room   | name | equipment   |
+------------+--------+------+-------------+
| 1349578297 | Room_1 | Joe  | Equipment_3 |
| 1349578297 | Room_1 | Jason| Equipment_4 |
+------------+--------+------+-------------+

DISTINCT doesn't solve the problem nor does GROUP BY tra.name, e.equipment

Thank you.

7
  • can you make sqlfiddle ? Commented Oct 13, 2012 at 4:29
  • Did you try DISTINCT equipment and GROUP BY name? Commented Oct 13, 2012 at 4:31
  • also better to show dump of tables, intead of select, or at least SHOW CREATE TABLE Commented Oct 13, 2012 at 4:35
  • eicto: sqlfiddle.com/#!2/d038f/2 Commented Oct 13, 2012 at 4:38
  • sorry it took me some time - first time I'm using sqlfiddle, thanks for the advise. Commented Oct 13, 2012 at 4:39

1 Answer 1

4

Sorry, but with your current database structure, i think there's no query to match your requirement. But you can check the group_concat() function, it can group the equipment and trainer name into a string. and then you can extract the value by use server code like php.

 SELECT t.from, r.room, group_concat(distinct tra.name), group_concat( distinct 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

If you can change the structure of table "training_equipment", add another column "trainer_id" then you can use the query below:

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_equipment te ON ( t.equipment = te.training_id)
LEFT JOIN Equipment e ON ( te.equipment_id = e.id )
LEFT JOIN Trainer tra ON ( te.trainer_id = tra.id )
WHERE t.id =1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.