我有两张桌子:
LANGUAGES
LANGUAGE_ID number(4),
LANG_NAME varchar2(30)
LABELS
LANGUAGE_ID number(4),
TEXT_ID varchar2(20),
LABEL_TEXT varchar2(100)
使用以下条目:
LANGUAGES:
LANGUAGE_ID | LANG_NAME
-----------------------
1 | german
2 | english
3 | spanish
4 | italian
LABELS:
LANGUAGE_ID | TEXT_ID | LABEL_TEXT
-----------------------------------
1 | A | Lorem ipsum
2 | A | Lorem ipsum
1 | B | Lorem ipsum
2 | B | Lorem ipsum
3 | B | Lorem ipsum
我的目标现在是选择所有标签,并且对于尚未输入标签的所有语言,都是空记录。结果应该是这样的:
LANGUAGE_ID | TEXT_ID | LABEL_TEXT
----------------------------------
1 | A | Lorem ipsum
2 | A | Lorem ipsum
3 | A |
4 | A |
1 | B | Lorem ipsum
2 | B | Lorem ipsum
3 | B | Lorem ipsum
4 | B |
如何使用Oracle SQL获得良好的语句来实现这一目标?
非常感谢!
答案 0 :(得分:2)
尝试
SELECT a.language_id, a.text_id, l.label_text
FROM
(
SELECT language_id, text_id
FROM languages CROSS JOIN
(
SELECT DISTINCT text_id FROM labels
) q) a LEFT JOIN labels l
ON a.language_id = l.language_id
AND a.text_id = l.text_id
ORDER BY a.text_id, a.language_id
输出:
| LANGUAGE_ID | TEXT_ID | LABEL_TEXT |
----------------------------------------
| 1 | A | Lorem ipsum |
| 2 | A | Lorem ipsum |
| 3 | A | (null) |
| 4 | A | (null) |
| 1 | B | Lorem ipsum |
| 2 | B | Lorem ipsum |
| 3 | B | Lorem ipsumh |
| 4 | B | (null) |
这是 SQLFiddle 演示
答案 1 :(得分:0)
这就是你想要的东西。
select a.language_id,b.text_id,b.label_text
from languages a left join labels b
on a.language_id=b.language_id