如何将两个具有不同内连接的咨询组合在一起

时间:2017-03-12 00:36:31

标签: mysql sql join

我有这张表:

TABLE tema

+---------+-------------+
| tema_id | nombre      |
+---------+-------------+
|       1 | Aprender    |
|       4 | Justicia    |
|       2 | Pensamiento |
|       3 | Vida        |
+---------+-------------+

表格frase(作者的短语)

+----+----------+----------------------------------------+
| id | autor_id | texto                                  |
+----+----------+----------------------------------------+
|  1 |        2 | Pienso, luego existo                   |
|  2 |        1 | Aprender sin pensar es trabajo perdido |
+----+----------+----------------------------------------+

表tema_frase(与表frase和tema的关系)

+----------+---------+
| frase_id | tema_id |
+----------+---------+
|        1 |       2 |
|        2 |       1 |
|        2 |       2 |
+----------+---------+

表作者(短语作者)

+----+-----------+----------------------------------------+
| id | nombre    | descripcion                            |
+----+-----------+----------------------------------------+
|  1 | Confucio  | Pensador chino                         |
|  2 | Descartes | Filósofo y matemático francés       |
+----+-----------+----------------------------------------+

我想如何展示: 表

+------------+------------------+----------------------------+
| autor      | phrases          | themes                     |
+------------+------------------+----------------------------+
| Confucio   | Pensador chino   | Pensamiento                |
| Descartes  | Filósofo y      | Pensamiento, aprender      |
+------------+------------------+----------------------------+

我知道有主题的短语和与作者的短语,但我不知道如何将它组合在一起因为我需要放入'FROM'两个表而我不能因为INNERS JOINS:

咨询短语和主题:

SELECT frase.texto as frase, group_concat(tema.nombre separator ", ") as temas FROM tema_frase INNER JOIN tema ON tema_frase.tema_id=tema.tema_id INNER JOIN frase ON frase.id=tema_frase.frase_id GROUP BY frase.texto

咨询短语和作者:

SELECT autor.nombre as Autor, frase.texto as Frases FROM frase INNER JOIN autor ON autor.id=frase.autor_id

1 个答案:

答案 0 :(得分:0)

您可以将url加入第一个查询,然后将作者添加到autor

group by