【问题标题】:Query from table based on some condition根据某些条件从表中查询
【发布时间】:2012-10-29 05:17:31
【问题描述】:

我有一个这样的场景,有 2 个表 table_1 和 table_2。两个表都有一个名为 column_1 的公共列(没有 foreign_Key 约束!!)。 Table_1 可以有一些额外的行,这些行在 table_2 中不存在(换句话说,table_2 是 table_1 的子集)。我想列出所有仅存在于 table_1 但不存在于 table_2 中的项目。 请帮助编写相同的sql查询。 提前致谢。

【问题讨论】:

    标签: sql oracle


    【解决方案1】:
    SELECT a.*
    FROM   table1 a
           LEFT JOIN table2 b
               on a.column_1 = b.column_1
    WHERE b.column_1 IS NULL
    

    如果这两个表不相关,最好在table1.column_1 和table2.column_1 上添加索引,这样就不需要全表扫描(会降低性能)

    【讨论】:

      【解决方案2】:
       select * from table1 
       inner join table2 on table1.column1=table2.column1
      

      【讨论】:

        【解决方案3】:
        select a.* from table1 a left outer join table2 b on a.col1=b.col1;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-28
          • 1970-01-01
          • 1970-01-01
          • 2018-09-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多