sql - Insert into with union -


i have problem. there 3 tables: t1, t2, t_target. t1 , t2 table have many different columns need id column both. t_target table has id column of course , another: project_no.

there ids appears in t1 , t2 too, don't want create duplicates between them, if id appears in both table have inserted t_target once if in t_target it's allowed act twice. other of criteria every newly inserted id must value 21 in 'project_no' column. so, e.g.:

t1:

id 2548 2566 2569 2843 2888 ... 

t2:

id 2557 2566 2569 2700 2913 2994 3018 5426 ... 

t_target:

id     project_no 2976   1 3331   7 4049   7 5426   8 5915   3 6253   10 ... 

and result want see:

t_target:

id     project_no 2548   21 2557   21 2566   21 2569   21 2700   21 2843   21 2888   21 2913   21 2976   1 2994   21 2018   21 3331   7 4049   7 5426   8 5426   21 5915   3 6253   10 ... 

so, tried code (it important here "not null" criteria because both of t_target columns primary key):

insert t_target (id, project_no)   select (select id   t1 id not null  union   select id   t2 id not null), 21  select * t_target 

the error message: "msg 512, level 16, state 1, line 2 subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression. statement has been terminated."

then tried values statement instead of first select , parentheses error same.

there similar problem: mysql query: how insert union? solution doesn't work me because indicates syntax error between value , select.

please, give me hand. thank you!

this should need

insert t_target             (id,              project_no) select id,        21   t1  id not null union select id,        21   t2  id not null  

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -