简体   繁体   English

插入联合

[英]Insert into with union

I have a problem. 我有个问题。 There are three tables: T1, T2, T_target. 有三个表:T1,T2,T_target。 T1 and T2 table have many different columns but I need only the ID column from both. T1和T2表有许多不同的列,但我只需要两列的ID列。 The T_target table has an ID column of course and another: project_No. T_target表当然有一个ID列,另一个是:project_No。

There are some IDs which appears in T1 and T2 too, but I don't want to create duplicates between them, if an ID appears in both table it have to be inserted into the T_target only once but if it is already in the T_target it's allowed to act twice. 有一些ID也出现在T1和T2中,但是我不想在它们之间创建重复项,如果两个表中都出现了ID,它只需要插入T_target一次但是如果它已经在T_target中那么它是允许两次行动。 The other of the criteria is every newly inserted ID must be value 21 in 'project_No' column. 另一个标准是每个新插入的ID必须在'project_No'列中为值21。 So, eg: 所以,例如:

T1: T1:

ID
2548
2566
2569
2843
2888
...

T2: T2:

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

T_target: T_target:

ID     project_No
2976   1
3331   7
4049   7
5426   8
5915   3
6253   10
...

And the result I want to see: 结果我想看到:

T_target: 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, I tried it with this code (it is important to be here "NOT NULL" criteria because both of T_target columns are primary key): 所以,我用这个代码尝试了它(因为两个T_target列都是主键,所以在这里“NOT NULL”标准很重要):

insert into T_target (ID, project_No)
  select (select ID
  from T1 where ID is not NULL
 union
  select ID
  from T2 where ID is not NULL), 21

select * from T_target

The error message: "Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated." 错误消息: “消息512,级别16,状态1,行2子查询返回的值超过1。当子查询跟随=,!=,<,<=,>,> =或子查询为时,不允许这样做用作表达式。声明已经终止。“

Then I tried with VALUES statement instead of the first SELECT and parentheses but the error is the same. 然后我尝试使用VALUES语句而不是第一个SELECT和括号,但错误是相同的。

There is a similar problem: mySQL query: How to insert with UNION? 有一个类似的问题: mySQL查询:如何使用UNION插入? but this solution doesn't work for me because it indicates syntax error between VALUE and SELECT. 但是这个解决方案对我不起作用,因为它表示VALUE和SELECT之间的语法错误。

Please, give me a hand. 拜托,帮我一把。 Thank you! 谢谢!

This should do what you need 这应该做你需要的

INSERT INTO T_target
            (ID,
             project_No)
SELECT ID,
       21
FROM   T1
WHERE  ID IS NOT NULL
UNION
SELECT ID,
       21
FROM   T2
WHERE  ID IS NOT NULL 

I think you have to amend that a little to avoid duplication of the ID's in the select statement. 我认为你必须稍微修改一下,以避免在select语句中重复ID。

INSERT INTO T_target
            (ID,
             project_No)

SELECT ID, 21 
FROM (
SELECT ID
FROM   T1
WHERE  ID IS NOT NULL
UNION
SELECT ID
FROM   T2
WHERE  ID IS NOT NULL 
) A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM