我不知道如何正确地写标题中的问题,但我会努力解释我要达到的目标。
这些是我的父表:
hgs_meaning hgs_word_types hgs_transliterations
+----+---------+ +----+-----------+ +----+-----------------+
| id | meaning | | id | word_type | | id | transliteration |
+----+---------+ +----+-----------+ +----+-----------------+
| 3 | man | | 4 | noun | | 5 | mnjw |
+----+---------+ +----+-----------+ +----+-----------------+
这些是我的连接表:
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
s、 但我不知道如何构造这样一个查询。
以下是我的疑问:
/* 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
任何帮助都将不胜感激。
1条答案
按热度按时间vawmfj5a1#
你可以简单地
JOIN
所有表及其相应的联接关系,然后SELECT
所需的特定列/表达式。还可以为中指定的列/表达式别名SELECT
子句,为它们显示不同的名称。同样,在多表查询的情况下,建议对表名使用别名,以提高代码的清晰度(可读性)并避免模棱两可的行为。