oracle USING 用法


提問

  • using(xx)中可以接多個列嗎?
  • using(xx)中的列可以接表名或別名嗎?
  • 在使用using的語句中,select * 可以使用嗎?
  • 如果表有別名t,select t.* from table1 t , table2 using(xx),t.* 可以使用嗎?
  • left join, right join, inner join, full outer join 可以和using一起用嗎?

我們看下語法就能知道答案了。

只看答案

  • using(xx)中可以接多個列,它是一種等值連接,可以轉換為 join ... on (x=y and xx=y);
  • using(xx)中的列不可以接表名或別名等限定語,默認就是指共同列,不能指定表名;
  • 在使用using的語句中,select * 可以使用,* 被擴展的順序為using中的列,左表中的其它列,右表中的其它列
  • 如果表有別名t,select t.* from table1 t , table2 using(xx),t.* 不可以使用,using中的列不能加限定詞
  • left join, right join, inner join, full outer join 可以和using一起使用,它就是簡單的等值連接

inner join 就是 join, outer join有 left, right, full outer join 三種
using的使用條件是兩表中列名和類型相同;
natural join更進一步,直接省掉了using,但作用和使用using相似;
inner 和 outer 的區別:內連接只返回匹配行,外連接還返回不滿足條件的行;


Syntax

USING ( Simple-column-Name [ , Simple-column-Name ]* )

USING clause definition

The USING clause specifies which columns to test for equality when two tables are joined. It can be used instead of an ON clause in the JOIN operations that have an explicit join clause.

If a column in the USING clause is referenced without being qualified by a table name, the column reference points to the column in the first (left) table if the join is an INNER JOIN or a LEFT OUTER JOIN. If it is a RIGHT OUTER JOIN, unqualified references to a column in the USING clause point to the column in the second (right) table.

When a USING clause is specified, an asterisk (*) in the select list of the query will be expanded to the following list of columns (in this order):

  • All the columns in the USING clause
  • All the columns of the first (left) table that are not specified in the USING clause
  • All the columns of the second (right) table that are not specified in the USING clause

Examples


SELECT * FROM COUNTRIES JOIN CITIES
     USING (COUNTRY);
     
SELECT * FROM COUNTRIES JOIN CITIES
    USING (COUNTRY, COUNTRY_ISO_CODE);
 
 SELECT * FROM oe.order_items oi JOIN oe.orders o
  USING(order_id);

select t.* from hr.employees t, hr.departments d
where t.department_id = d.department_id;

select * from hr.employees t inner join hr.departments d
using(department_id);

select * from hr.employees t left outer join hr.departments d
using(department_id);

select * from hr.employees t right outer join hr.departments d
using(department_id);

select * from hr.employees t full outer join hr.departments d
using(department_id);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM