MYSQL - Insert If Table is empty

后端 未结 1 1884
礼貌的吻别
礼貌的吻别 2021-01-21 15:44

I want to insert some data on my table if the table is empty (0 rows).

I tried this:

IF NOT EXISTS(SELECT 1 FROM `account_types`)
THEN
       INSERT INTO         


        
相关标签:
1条回答
  • 2021-01-21 16:14
    INSERT INTO `account_types`(`type`, `group`)
    SELECT * FROM
    (SELECT '400' `type`, 'test' `group` UNION ALL
     SELECT '401' `type`, 'test2' `group` UNION ALL
     SELECT '402' `type`, 'test3' `group`) A
    WHERE NOT EXISTS (SELECT NULL FROM account_types B WHERE A.type=B.type);
    

    Demo

    http://sqlfiddle.com/#!9/d1e80e/1

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