mysql - 如何使用来自 parent table 的值将连接 table 连接到另一个连接 table?

mysql - How to join junctional table to another junctional table with values from parent tables?

不知道如何在标题中正确写出问题,但我会尽力解释我想要达到的目标。

这些是我的 parent tables:

 hgs_meaning         hgs_word_types        hgs_transliterations
 +----+---------+    +----+-----------+    +----+-----------------+
 | id | meaning |    | id | word_type |    | id | transliteration |
 +----+---------+    +----+-----------+    +----+-----------------+
 | 3  | man     |    | 4  | noun      |    | 5  | mnjw            |
 +----+---------+    +----+-----------+    +----+-----------------+

这些是我的 tables:

 junc_meaning_word_type
 +----+------------+--------------+
 | id | meaning_id | word_type_id |
 +----+------------+--------------+
 | 2  | 3          | 4            |
 +----+------------+--------------+

 junc_transliteration_meaning_word_type
 +----+--------------------+----------------------+
 | id | transliteration_id | meaning_word_type_id |
 +----+--------------------+----------------------+
 | 1  | 5                  | 2                    |
 +----+--------------------+----------------------+

我知道如何进行 SELECT JOIN 查询以获得 junc_meaning_word_type table 但不是 junc_transliteration_meaning_word_type

的结果

我知道如何得到这个结果:

 +----+-----------------+-------------------+
 | id | transliteration | meaning_word_type |
 +----+-----------------+-------------------+
 | 1  | mnjw            | 2                 |
 +----+-----------------+-------------------+

我正在努力实现这个结果:

 +----+--------------------+------------+--------------+
 | id | transliteration_id | meaning_id | word_type_id |
 +----+--------------------+------------+--------------+
 | 1  | mnjw               | man        | noun         |
 +----+--------------------+------------+--------------+

如何做到这一点。我猜我需要使用嵌套 SELECT 查询(子查询)或多个 JOIN,但我不知道如何构造这样的查询。

这是我的查询:

/* hgs_meanings and hgs_word_types join to junc_meaning_word_type */

SELECT junc_meaning_word_type.id, hgs_word_types.word_type, hgs_meanings.meaning
FROM junc_meaning_word_type
JOIN hgs_word_types ON junc_meaning_word_type.word_type_id = hgs_word_types.id
JOIN hgs_meanings ON junc_meaning_word_type.meaning_id = hgs_meanings.id 

/* hgs_transliterations and junc_meaning_word_type join to junc_transliteration_meaning */

SELECT junc_transliteration_meaning.id, hgs_transliterations.transliteration, junc_meaning_word_type.id
FROM junc_transliteration_meaning
JOIN hgs_transliterations ON junc_transliteration_meaning.transliteration_id = hgs_transliterations.id
JOIN junc_meaning_word_type ON junc_transliteration_meaning.meaning_word_type_id = junc_meaning_word_type.id

如有任何帮助,我们将不胜感激。

您可以简单地 JOIN 所有 table 及其适当的联接关系,然后 SELECT 您需要的特定 columns/expressions。您还可以为 SELECT 子句中指定的 columns/expressions 添加别名,以显示不同的名称。

同样,为了代码清晰(可读性)和避免歧义行为,建议在 table 名称上使用 Aliasing,以防出现多个 table 查询.

SELECT junctrans.id, 
       trans.transliteration AS transliteration_id, 
       mean.meaning AS meaning_id, 
       typ.word_type AS word_type_id 
FROM 
  junc_transliteration_meaning_word_type AS junctrans
JOIN junc_meaning_word_type AS juncmean 
  ON juncmean.id = junctrans.meaning_word_type_id 
JOIN hgs_transliterations AS trans
  ON trans.id = junctrans.transliteration_id 
JOIN hgs_meaning AS mean 
  ON mean.id = juncmean.meaning_id 
JOIN hgs_word_types AS typ 
  ON typ.id = juncmean.word_type_id