需要帮助来存储三栏的价值

时间:2017-01-17 22:34:21

标签: sql oracle oracle11g

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
--My Record is comming bellow 
ID          client_p_email                             client_s_email                                         customer_mail
-----       ----------------------------      ----------------------------                          ---------------------------
703         test01@aol.com                          prod01@gamil.com                                            dev01@yahoo.com.                                    
623         ra.ben@yahoo.com                   ra.ben@yahoo.com                                           ea.bowens@gmail.com                                      
965         eighteenman@aol.com               eighteenman@aol.com                                  eighteenman@aol.com                                        
270         aunkurr1@icloud.com                   amirbhai@icloud.com                                 amirbhai@me.com                                         
719         rah1021@yahoo.com                  rh1021@yahoo.com                                          mars77@vrizon.net     

我想要什么? 我想在一列中显示所有电子邮件。但是如果你注意到703 id必须存储三次而623只需要存储两次因为两个电子邮件是相同的。 并且965将存储一次,因为所有都是相同的。 请建议我如何创建一个匿名块,以存储我想要的价值 请帮忙

1 个答案:

答案 0 :(得分:0)

假设您要在以下表格中插入数据:

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

假设您已经有了提供该结果的查询,您可能需要:

insert into allEmailTable(id, mail)
with yourQuery(ID, client_p_email, client_s_email, customer_mail) as (
  select 703        , 'test01@aol.com'           ,'prod01@gamil.com'   , 'dev01@yahoo.com' from dual union all                                   
  select 623        , 'ra.ben@yahoo.com'         ,'ra.ben@yahoo.com'   ,  'ea.bowens@gmail.com' from dual union all                                      
  select 965        , 'eighteenman@aol.com'      ,'eighteenman@aol.com',  'eighteenman@aol.com' from dual union all                                        
  select 270        , 'aunkurr1@icloud.com'      ,'amirbhai@icloud.com',  'amirbhai@me.com' from dual union all                                         
  select 719        , 'rah1021@yahoo.com'        ,'rh1021@yahoo.com'   ,  'mars77@vrizon.net' 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 
     )

结果:

SQL> select * from allEmailTable;

        ID MAIL
---------- --------------------
       270 amirbhai@icloud.com
       270 amirbhai@me.com
       270 aunkurr1@icloud.com
       623 ea.bowens@gmail.com
       623 ra.ben@yahoo.com
       703 dev01@yahoo.com
       703 prod01@gamil.com
       703 test01@aol.com
       719 mars77@vrizon.net
       719 rah1021@yahoo.com
       719 rh1021@yahoo.com
       965 eighteenman@aol.com

12 rows selected.

您的查询将是:

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 
     )