0

我有两张桌子:

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 的性能良好的语句来实现这一点?

非常感谢你!

4

2 回答 2

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演示

于 2013-05-30T07:11:16.790 回答
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
于 2013-05-30T07:18:02.997 回答