MySQL多连接&删除重复项

时间:2012-10-13 04:25:58

标签: mysql select join

快速更新 - SQLFiddle链接:http://sqlfiddle.com/#!2/d038f/2

我认为这是一个相对直接的...

我共有7个表,3个'主'表,另外3个管理'多对多'关系,另有3个是主表,更准确地说:

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

也无法解决问题

谢谢。

1 个答案:

答案 0 :(得分:4)

很抱歉,但是根据您当前的数据库结构,我认为没有符合您要求的查询。 但是你可以检查group_concat()函数,它可以将设备和训练器名称分组成一个字符串。然后你可以使用像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

如果您可以更改表“training_equipment”的结构,请添加另一列“trainer_id”,然后您可以使用以下查询:

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