MySql insert the results of a select

前端 未结 3 599
我寻月下人不归
我寻月下人不归 2020-12-05 17:36

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          


        
相关标签:
3条回答
  • 2020-12-05 18:03

    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 
      )
    
    0 讨论(0)
  • 2020-12-05 18:05

    remove the 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;
    
    0 讨论(0)
  • 2020-12-05 18:09

    You dont need the VALUES in your query.

    0 讨论(0)
提交回复
热议问题