sql >> データベース >  >> RDS >> Oracle

3列の値を保存するためのヘルプが必要

    次のようなテーブルにデータを挿入するとします。

    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        , '[email protected]'           ,'[email protected]'   , '[email protected]' from dual union all                                   
      select 623        , '[email protected]'         ,'[email protected]'   ,  '[email protected]' from dual union all                                      
      select 965        , '[email protected]'      ,'[email protected]',  '[email protected]' from dual union all                                        
      select 270        , '[email protected]'      ,'[email protected]',  '[email protected]' from dual union all                                         
      select 719        , '[email protected]'        ,'[email protected]'   ,  '[email protected]' 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 [email protected]
           270 [email protected]
           270 [email protected]
           623 [email protected]
           623 [email protected]
           703 [email protected]
           703 [email protected]
           703 [email protected]
           719 [email protected]
           719 [email protected]
           719 [email protected]
           965 [email protected]
    
    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 
         )
    



    1. SQL SELECTステートメントで会計年度を計算しますか?

    2. phpのJavaScript変数値にアクセスしてmysqlに保存します

    3. 複数のホスト上のPHPサイトのセッションを処理するための最良の方法は何ですか?

    4. ガレラクラスターを保護する方法-8つのヒント