【问题标题】:Need help to store value from three column需要帮助来存储三列中的值
【发布时间】:2017-06-02 04:08:03
【问题描述】:
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 将存储一次,因为它们都是相同的。 请建议我如何创建一个匿名块来存储我想要的值
请帮忙

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

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

    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 
         )
    

    【讨论】:

    • 抱歉误会,我希望我的结果存储在 new_email_col 中,如下所示:703 test01@aol.com——这一行又是 703 prod01@gamil.com,所以 703 将有 3 个插入跨度>
    • 我可以通过 CURSOR 插入吗?
    • 插入什么?您显示了来自两个表的查询,并说您需要另一列。在哪里?您需要更新表的数据(哪一个?)还是需要一个选择来返回选择中的所有列,此外,还需要一个包含单个字符串中所有邮件的列?
    • 另外,我注意到您在此问题之前发布了 10 个问题,但从未接受过答案。你从来没有一个好的答案吗?如果你连“谢谢”都不想说,人们为什么要帮助你?
    • 我有另一个表,该表有列,可以说是 ALL_EMAIL,所以我在 all_email 列中插入了如下图 703 test01@aol.com——这一行又是 703 prod01@gamil.com,所以 703 将有 3 个插入,这将是 3 个单独的行
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多