Id Name
1 Apple
2 Mango
Id Locale Name_In_Lang
1 es-ES Apple[Spanish]
1 it-IT Apple[Italian]
2 it-IT Mango[Italian]
加入表格并获得以下输出:
Id Locale Name_In_Lang Name
1 Apple
1 es-ES Apple[Spanish] Apple
1 it-IT Apple[Italian] Apple
2 Mango
2 it-IT Mango[Italian] Mango
我有以下查询...
Select a.id, b.locale, b.name_in_lang, a.name
from TableA a
Left Outer Join TableB b on ( a.id = b.id)
......我只得到:
Id Locale Name_In_Lang Name
1 es-ES Apple[Spanish] Apple
1 it-IT Apple[Italian] Apple
2 it-IT Mango[Italian] Mango
有什么建议吗?
答案 0 :(得分:5)
您可以将实际结果与所需的额外行结合起来,如下所示:
SELECT a.id, b.locale, b.name_in_lang, a.name
FROM TableA a
LEFT OUTER JOIN TableB b ON a.id = b.id
UNION
SELECT a.id, '' AS locale, '' AS name_in_lang, a.name
FROM Table a
UNION中的第二个查询将在表A中为每个记录提供一行,其中包含空的语言环境和已翻译的名称。
答案 1 :(得分:0)
如果要为TableA中的每个条目添加一行,可以使用UNION子句:
SELECT
a.id, b.locale, b.name_in_lang, a.name
FROM
TableA a
LEFT OUTER JOIN
TableB b ON a.id = b.id
UNION
SELECT
id, '', '', name
FROM
TableA
这会在结果集中添加一个没有任何语言环境信息的条目。
答案 2 :(得分:0)
SELECT a.Id, NULL AS Locale, NULL AS Name_IN_Lang, a.Name
FROM TableA a
UNION ALL
SELECT a.Id, b.Locale, b.Name_In_Lang, a.Name
FROM TableA a
INNER JOIN TableB b
ON a.Id = b.Id
ORDER BY Id, Name, Locale