且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅在表为空时执行mysql插入

更新时间:2023-12-01 14:03:40

您的语句中有语法错误:

You have a syntax error in your statement:

INSERT INTO `statuses`
    (SELECT  'Something', 'Something else', 123
     WHERE NOT EXISTS (SELECT * FROM `statuses`)
    ) union all
    (SELECT 'Something', 'Something else', 234
     WHERE NOT EXISTS (SELECT * FROM `statuses`)
    );

在这种情况下,您需要重复where两次,每个子查询一次.您也可以这样做:

You need to repeat the where twice in this case, once for each subquery. You can also do:

INSERT INTO `statuses`
    select t.*
    from ((SELECT  'Something' as col1, 'Something else' as col2, 123 as col3
          ) union all
          (SELECT 'Something', 'Something else', 234
          )
         ) t
    WHERE NOT EXISTS (SELECT * FROM `statuses`);

在此版本中,您需要为列分配名称.

In this version, you need to assign names to the columns.

或者,您可以只使用两个单独的插入语句.

Or, you could just use two separate insert statements.