具有concat值的倍数关系

时间:2019-06-24 10:47:19

标签: mysql sql foreign-keys relationship has-many-polymorphs

在我的基础上,我有这种关系(image):

+------------+------------+-------------+------------------+------------+------------+
| fiche_jeux |            |             | fiche_jeux_infos |            |            |
+------------+------------+-------------+------------------+------------+------------+
| id         | name       |             | jeux_id          | date       | category   |
+------------+------------+-------------+------------------+------------+------------+
| 1          | Destiny 2  |             | 1                | 2019-01-01 | destiny-2  |
+------------+------------+-------------+------------------+------------+------------+
| 2          | Overwatch  |             | 2                | 2019-01-01 | overwatch  |
+------------+------------+-------------+------------------+------------+------------+
| 3          | Mario kart |             | 3                | 2019-01-01 | mario-kart |
+------------+------------+-------------+------------------+------------+------------+

并与:

有关
+-------------+------+-------------+---------------------+--------------+
| plateformes |      |             | plateformes_details |              |
+-------------+------+-------------+---------------------+--------------+
| id          | name |             | id_plateformes      | id_fiche_jeu |
+-------------+------+-------------+---------------------+--------------+
| 1           | pc   |             | 1                   | 1            |
+-------------+------+-------------+---------------------+--------------+
| 2           | xbox |             | 1                   | 1            |
+-------------+------+-------------+---------------------+--------------+
| 3           | ps4  |             | 2                   | 1            |
+-------------+------+-------------+---------------------+--------------+
|             |      |             | 3                   | 2            |
+-------------+------+-------------+---------------------+--------------+
|             |      |             | 2                   | 2            |
+-------------+------+-------------+---------------------+--------------+
|             |      |             | 1                   | 2            |
+-------------+------+-------------+---------------------+--------------+

通过此查询,我获得了这样的concat“平台”:

SELECT GROUP_CONCAT(plateformes.name) as plateforme_name FROM plateformes 

LEFT OUTER JOIN plateformes_details ON plateformes.id = plateformes_details.id_plateformes 
RIGHT JOIN fiche_jeux ON fiche_jeux.id = plateformes_details.id_fiche_jeu 
LEFT JOIN fiche_jeux_infos ON fiche_jeux_infos.jeux_id = fiche_jeux.id 

GROUP BY fiche_jeux.name

效果很好,结果:

+-----------------+
| plateforme_name |
+-----------------+
| pc,ps4,xbox     |
| pc              |
| pc,xbox         |
| pc,ps4,xbox     |
| pc,ps4,xbox     |
|                 |
|                 |
| ....            |
+-----------------+

现在,我如何才能以相同的结果组合模式和流派?

+-------+-------+-------------+---------------+--------------+
| modes |       |             | modes_details |              |
+-------+-------+-------------+---------------+--------------+
| id    | name  |             | id_modes      | id_fiche_jeu |
+-------+-------+-------------+---------------+--------------+
| 1     | solo  |             | 1             | 1            |
+-------+-------+-------------+---------------+--------------+
| 2     | pvp   |             | 1             | 1            |
+-------+-------+-------------+---------------+--------------+
| 3     | multi |             | 2             | 1            |
+-------+-------+-------------+---------------+--------------+
|       |       |             | 3             | 2            |
+-------+-------+-------------+---------------+--------------+
|       |       |             | 2             | 2            |
+-------+-------+-------------+---------------+--------------+
|       |       |             | 1             | 2            |
+-------+-------+-------------+---------------+--------------+

+--------+--------+------------+----------------+--------------+
| genres |        |            | genres_details |              |
+--------+--------+------------+----------------+--------------+
| id     | name   |            | id_genres      | id_fiche_jeu |
+--------+--------+------------+----------------+--------------+
| 1      | action |            | 1              | 3            |
+--------+--------+------------+----------------+--------------+
| 2      | fps    |            | 3              | 3            |
+--------+--------+------------+----------------+--------------+
| 3      | rpg    |            | 2              | 1            |
+--------+--------+------------+----------------+--------------+
| ...    | ...    |            |                |              |
+--------+--------+------------+----------------+--------------+

我已经尝试过了,但是结果不好:

SELECT GROUP_CONCAT(plateformes.name) as plateforme_name, GROUP_CONCAT(modes.name) as modes_name, GROUP_CONCAT(genres.name) as genre_name FROM plateformes 

 INNER JOIN plateformes_details ON plateformes.id = plateformes_details.id_plateformes 

 LEFT OUTER JOIN fiche_jeux ON fiche_jeux.id = plateformes_details.id_fiche_jeu 
 LEFT OUTER JOIN fiche_jeux_infos ON fiche_jeux_infos.jeux_id = fiche_jeux.id 

 INNER JOIN modes_details ON modes_details.id_fiche_jeu = fiche_jeux.id 
 INNER JOIN modes ON  modes.id = modes_details.id_modes

 INNER JOIN genres_details ON genres_details.id_fiche_jeu = fiche_jeux.id 
 INNER JOIN genres ON  genres.id = genres_details.id_genres

GROUP BY fiche_jeux.name

结果:

+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
| plateforme_name                       |  | modes_name                                      |  | genre_name                                            |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
| xbox,ps4,pc                           |  | coopération,coopération,coopération             |  | aventure,aventure,aventure                            |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
| ios,ios,android,android,pc,pc         |  | pvp,multijoueur,pvp,multijoueur,pvp,multijoueur |  | mmorpg,mmorpg,mmorpg,mmorpg,mmorpg,mmorpg             |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
| ps4,ps4,pc,pc,xbox,xbox,stadia,stadia |  | solo,solo,solo,solo,solo,solo,solo,solo         |  | action,aventure,action,aventure,action,aventure,ac... |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
|                                       |  |                                                 |  |                                                       |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
|                                       |  |                                                 |  |                                                       |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
|                                       |  |                                                 |  |                                                       |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+
|                                       |  |                                                 |  |                                                       |
+---------------------------------------+--+-------------------------------------------------+--+-------------------------------------------------------+

此结果包含太多重复值:(

我想要这样:

+--------------------+--+-----------------+--+-----------------+
| plateforme_name    |  | modes_name      |  | genre_name      |
+--------------------+--+-----------------+--+-----------------+
| xbox,ps4,pc        |  | coopération     |  | aventure        |
+--------------------+--+-----------------+--+-----------------+
| ios,android,pc     |  | pvp,multijoueur |  | mmorpg          |
+--------------------+--+-----------------+--+-----------------+
| ps4,pc,xbox,stadia |  | solo            |  | action,aventure |
+--------------------+--+-----------------+--+-----------------+
|                    |  |                 |  |                 |
+--------------------+--+-----------------+--+-----------------+
|                    |  |                 |  |                 |
+--------------------+--+-----------------+--+-----------------+
|                    |  |                 |  |                 |
+--------------------+--+-----------------+--+-----------------+
|                    |  |                 |  |                 |
+--------------------+--+-----------------+--+-----------------+

0 个答案:

没有答案