Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Tabel turunan Oracle sql - aliasing opsional

Anda hanya perlu alias, saat Anda mereferensikan kolom yang tidak didefinisikan secara unik. Ini berarti bahwa kolom ada di lebih dari satu tabel/tabel turunan. Referensi bisa dalam pernyataan pilih, atau bergabung. Jika semua kolom unik, maka Anda tidak memerlukan alias.

Saya lebih suka alias sepanjang waktu untuk kejelasan, dan karena ini membantu Intellisense di PL/SQL.

--ALIAS needed, because the 'a' column referenced is not unique
--this will throw an error
select a, a, b, c
  from (select 'A1' as a, 'B1' as b, 'C1' as c from dual),
       (select 'A2' as a from dual);
--this will not throw an error
select t1.a, t2.a, b,c
  from (select 'A1' as a, 'B1' as b, 'C1' as c from dual) t1,
       (select 'A2' as a from dual) t2;
;

--ALIAS not needed for join, because all referenced columns are unique
select a, b, c, d, e, f
  from (select 'A' as a, 'B' as b, 'C' as c from dual)
  join (select 'D' as d, 'E' as e, 'F' as f from dual)
    on a = d;

--ALIAS needed for join, because the 'x' column referenced is not unique
--this will throw an error
select a
  from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual)
  join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual)
    on x = x;
--this will not throw an error
select a
  from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual) t1
  join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual) t2
    on t1.x = t2.x;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL:Cara menambahkan nilai menurut kolom indeks

  2. Pisahkan String dengan baris baru dan tambahkan array di PL sql Oracle

  3. Kembalikan Hasil Kueri sebagai Daftar Dipisahkan Koma di Oracle

  4. Kolom pengembalian Oracle SQL dihitung dari kolom yang ada

  5. Output yang diinginkan dengan data tabel yang diberikan