Execute INSERT if table is empty?

前端 未结 3 1284
春和景丽
春和景丽 2020-12-03 14:22

Is there a way to do an insert under a count condition, something like:

INSERT INTO my_table (colname) VALUES(\'foo\') IF COUNT(my_table) < 1
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 15:00

    Use SELECT instead of VALUES to be able to expand the query with a WHERE clause.

    EXISTS is a better & faster test than COUNT

    INSERT INTO my_table (colname)
    SELECT 'foo'
    WHERE NOT EXISTS (SELECT * FROM my_table)
    

提交回复
热议问题