MySQL MySql插入一个select的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4472929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:02:20  来源:igfitidea点击:

MySql insert the results of a select

mysqlselectinsert

提问by Roch

I would like to know if I can run a request like that:

我想知道我是否可以运行这样的请求:

INSERT INTO t2 (a, b) 
VALUES (
 SELECT a, b
 FROM `t1` AS o
 WHERE o.id NOT 
 IN (
  SELECT a
  FROM t2 
  )
)

The idea is to fill the t2 with some data from the t1, but I must be wrong on the syntax.

这个想法是用来自 t1 的一些数据填充 t2,但我在语法上一定是错误的。

Thanks for your help

谢谢你的帮助

回答by Joe Stefanelli

You don't use the VALUESkeyword when inserting from a SELECTstatement.

VALUESSELECT语句插入时不使用关键字。

INSERT INTO t2 (a, b) 
 SELECT a, b
 FROM `t1` AS o
 WHERE o.id NOT 
 IN (
  SELECT a
  FROM t2 
  )

回答by ajreal

remove the values

去除那个 values

like

喜欢

INSERT INTO t2 (a, b) 
SELECT a, b
FROM `t1` AS o
WHERE o.id NOT 
IN 
(
  SELECT a
  FROM t2 
);

OR a more readble format

或者更易读的格式

INSERT INTO t2 (a, b) 
SELECT o.a, o.b
FROM `t1` AS o
LEFT JOIN t2 ON o.id=t2.a
WHERE t2.a IS NULL;

回答by Travyguy9

You dont need the VALUES in your query.

您不需要查询中的 VALUES。