sql语句 - 创建一个视图,但id列为每个标题分配每个数字

时间:2014-01-10 14:26:22

标签: sql oracle

我需要一些声明的帮助。视图已创建,但该语句不是将相关的number_id应用于描述,而是将每个number_id编号应用于创建1000个数据行而不是200个左右的每个描述...我希望这是明确的吗?

CREATE OR REPLACE FORCE VIEW MAP_SEARCH_VW
(
   number_id,
   DESCRIPTION,
   CATEGORY,
   SEARCH_DESC,
   SEARCH_TAG
)
AS
   SELECT   table1.number_id,
            table2.description,
            'Category',
            table3.search_desc,
            table3.search_tag
     FROM   table3, table2, table1
    WHERE   table3.description = table2.description;

DROP PUBLIC SYNONYM MAP_SEARCH_VW;

CREATE PUBLIC SYNONYM MAP_SEARCH_VW FOR Database.MAP_SEARCH_VW;

1 个答案:

答案 0 :(得分:2)

SELECT   table1.number_id,
         table2.description,
         'Category',
         table3.search_desc,
         table3.search_tag
  FROM   table3, table2, table1
 WHERE   table3.description = table2.description;

是的,您没有在表1和表2之间指定任何关系,因此 table1的每个记录都与来自table2的每个记录匹配。

使用显式 JOIN条件更具可读性(并且你会减少错误):

SELECT   table1.number_id,
         table2.description,
         'Category',
         table3.search_desc,
         table3.search_tag
  FROM   table1
  JOIN   table2 on table1.somefield = table2.somefield
  JOIN   table3 on table3.description = table2.description;