簡體   English   中英

如何在 Oracle 中選擇 2 個表?

[英]How can I select 2 tables in Oracle?

我有 2 個表,第一個叫做“test1”:

COL1
Blabla
foo
lib

第二個是 "test2" :

COL2 
test
gg 
op 

我想對兩個表(使用 col1=col2)使用 select 語句(使用 Oracle)並按 col1(或 col2)對表進行排序。

我想得到

COL1
Blabla
foo
lib
test
gg 
op

先感謝您。

除非我誤解了這是非常基本的 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/以更熟悉連接

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>

但是, sort 與您想要的輸出不同。 除非有一個列可以用來對數據進行排序,嗯,你很不走運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM