MySql 插入一个选择的结果

2022-01-09 00:00:00 select insert mysql

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

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 
  )
)

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

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

感谢您的帮助

推荐答案

SELECT 语句插入时不要使用 VALUES 关键字.

You don't use the VALUES keyword when inserting from a SELECT statement.

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

相关文章