sql - MySQL query to select from multiple table and insert in one -


there 5 tables: a, b, c, d, e. let each table contain 'email' field. want insert emails in present in of c, d, e not in b.

sample queries:

create table (email varchar(100)); create table b (email varchar(100)); create table c (email varchar(100)); create table d (email varchar(100)); create table e (email varchar(100));  insert b (email) values ('a@b.com'), ('c@d.com'); insert c (email) values ('a@b.com'), ('e@f.com'), ('e@f.com'); insert d (email) values ('a@b.com'), ('c@d.com'), ('g@h.com'); insert e (email) values ('c@d.com'), ('g@h.com'), ('i@j.com'), ('i@j.com'); 

this tried:

insert (email) select c.email c left join b on c.email = b.email b.email null union distinct select d.email d left join b on d.email = b.email b.email union distinct select e.email email e left join b on e.email = b.email b.email null; 

the following direct translation of description:

insert a(email)     select email     ((select email c) union           (select email d) union           (select email e)          ) cde     not exists (select 1 b b.email = cde.email); 

note: uses union intentionally remove duplicates. if know there no duplicates in c, d, e or if want duplicates, use union all instead.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -