【问题标题】:How can I select 2 tables in Oracle?如何在 Oracle 中选择 2 个表?
【发布时间】:2020-02-07 16:42:44
【问题描述】:

我有两张桌子,第一张叫做“test1”:

COL1
Blabla
foo
lib

第二个是“test2”:

COL2 
test
gg 
op 

我想对两个表(col1=col2)使用 select 语句(使用 Oracle),并按 col1(或 col2)对表进行排序。

我想得到

COL1
Blabla
foo
lib
test
gg 
op

提前谢谢你。

【问题讨论】:

  • 您需要显示重复项吗?根据您应该合并这两个表

标签: oracle join select


【解决方案1】:

除非我误解了这是非常基本的 sql 东西

select * from test1 t1 inner join test2 t2 on t1.col1=t2.col2 order by col1

将为您提供 test1 和 test2 中满足条件 col1=col2 的所有列,然后按 col1 排序

如果您需要特定的输出,请指定您需要的列,例如

select t1.Col1, t1.BlaBla, t1.foo, t1.Lib, t2.test, t2.gg, t2.op from test1 t1 inner join test2 t2 on t1.col1=t2.col2 order by col1

我假设您的示例包含您的数据而不是列名,因此请修改 sql 语句以匹配您的数据库

考虑访问网站http://www.sql-join.com/ 以更熟悉连接

【讨论】:

    【解决方案2】:

    Union,对吧?

    SQL> select col1 from test1
      2  union
      3  select col2 from test2
      4  order by 1;
    
    COL1
    ------
    blabla
    foo
    gg
    lib
    op
    test
    
    6 rows selected.
    
    SQL>
    

    不过,排序与您想要的输出不同。除非有一个列可以用来对数据进行排序,嗯,否则你很不走运。

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多