我的場景:有3個表格用於存儲電視節目信息;季節,情節和episode_translation。SQL LEFT JOIN幫助
我的數據:有3季,每季3集,但只有一集的翻譯。
我的目標:我想獲得一個節目的所有季節和劇集列表。如果有指定語言的翻譯,請顯示它,否則顯示爲空。
我試圖讓意甲1信息語言1:
SELECT
season_number AS season,number AS episode,name
FROM
season NATURAL JOIN episode
NATURAL LEFT JOIN episode_trans
WHERE
id_serie=1 AND
id_lang=1
ORDER BY
season_number,number
結果:
+--------+---------+--------------------------------+
| season | episode | name |
+--------+---------+--------------------------------+
| 3 | 3 | Episode translated into lang 1 |
+--------+---------+--------------------------------+
預期的結果
+-----------------+--------------------------------+
| season | episode| name |
+-----------------+--------------------------------+
| 1 | 1 | NULL |
| 1 | 2 | NULL |
| 1 | 3 | NULL |
| 2 | 1 | NULL |
| 2 | 2 | NULL |
| 2 | 3 | NULL |
| 3 | 1 | NULL |
| 3 | 2 | NULL |
| 3 | 3 | Episode translated into lang 1 |
+--------+--------+--------------------------------+
全部DB轉儲 http://pastebin.com/Y8yXNHrH
這應該被標記爲MySQL – 2010-06-15 14:09:28
哪個版本的SQL? – AllenG 2010-06-15 14:11:15
你在where子句中指定id_lang = 1。如果你把它拿出來,它是否會返回預期的結果? – Shaded 2010-06-15 14:12:15