Oracle
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Oracle

Cần trợ giúp để lưu trữ giá trị từ ba cột

Giả sử bạn muốn chèn dữ liệu vào một bảng như:

create table allEmailTable  (id number, mail varchar2(100))

Giả sử bạn đã có truy vấn đưa ra kết quả đó, bạn có thể cần:

insert into allEmailTable(id, mail)
with yourQuery(ID, client_p_email, client_s_email, customer_mail) as (
  select 703        , 'example@sqldat.com'           ,'example@sqldat.com'   , 'example@sqldat.com' from dual union all                                   
  select 623        , 'example@sqldat.com'         ,'example@sqldat.com'   ,  'example@sqldat.com' from dual union all                                      
  select 965        , 'example@sqldat.com'      ,'example@sqldat.com',  'example@sqldat.com' from dual union all                                        
  select 270        , 'example@sqldat.com'      ,'example@sqldat.com',  'example@sqldat.com' from dual union all                                         
  select 719        , 'example@sqldat.com'        ,'example@sqldat.com'   ,  'example@sqldat.com' from dual
)
select distinct ID, mail
from (
      select id, client_p_email as mail from yourQuery UNION
      select id, client_s_email         from yourQuery UNION
      select id, customer_mail          from yourQuery 
     )

Kết quả:

SQL> select * from allEmailTable;

        ID MAIL
---------- --------------------
       270 example@sqldat.com
       270 example@sqldat.com
       270 example@sqldat.com
       623 example@sqldat.com
       623 example@sqldat.com
       703 example@sqldat.com
       703 example@sqldat.com
       703 example@sqldat.com
       719 example@sqldat.com
       719 example@sqldat.com
       719 example@sqldat.com
       965 example@sqldat.com

12 rows selected.

Truy vấn của bạn sẽ là:

insert into allEmailTable(id, mail)
with yourQuery(ID, client_p_email, client_s_email, customer_mail) as (
  SELECT DISTINCT 
                    clt.id,
                    clt.client_p_email,
                    clt.client_s_email,
                    cus.customer_mail
  from  client clt,
         customers cus
where clt.id=cus.id
)
select distinct ID, mail
from (
      select id, client_p_email as mail from yourQuery UNION
      select id, client_s_email         from yourQuery UNION
      select id, customer_mail          from yourQuery 
     )



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Làm thế nào để tạo hàm trong PL / SQL?

  2. Làm thế nào bạn có thể buộc một hàm trong mệnh đề where thực thi một lần trong oracle?

  3. Kết nối WildFly jdbc với Oracle

  4. Oracle Thêm 1 giờ trong SQL

  5. Làm cách nào để tải Oracle SCHEMA dưới dạng tập lệnh DDL với DBMS_METADATA (và SCHEMA_EXPORT)