sql >> Databasteknik >  >> RDS >> Oracle

Behöver hjälp med att lagra värde från tre kolumner

Säg att du vill infoga data i en tabell som:

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

Förutsatt att du redan har din fråga som ger det resultatet, kan du behöva:

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 
     )

Resultatet:

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.

Din fråga blir:

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. Använd IN-direktivet för att söka med ett förberett utlåtande

  2. Fyll i datamängd med tabellnamn från lagrad procedur

  3. Allvarligt fel påträffades under dataläsning

  4. Hur man skriver flera kolumner i klausul med sqlalchemy