sql >> Database teknologi >  >> RDS >> Oracle

Har brug for hjælp til at gemme værdi fra tre kolonner

Lad os sige, at du vil indsætte data i en tabel som:

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

Forudsat at du allerede har din forespørgsel, der giver det resultat, kan du have brug for:

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 
     )

Resultatet:

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.

Din forespørgsel vil være:

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. Sådan kalder du en procedure, der bruger den samme tabel i efter trigger

  2. Sletning af tabeller med mysqli_multi_query()

  3. Lagring af følsomme data sikkert i en database

  4. Fjern efterfølgende decimal nul