使用仅MySQL,我正在查看是否有可能仅在表是新的情况下运行插入语句。我成功地创建了一个用户变量来查看表是否存在。问题在于,您无法在插入语句中使用“WHERE”。有没有想法让这个工作?
// See if the "country" table exists -- saving the result to a variable
SELECT
@table_exists := COUNT(*)
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'country';
// Create the table if it doesn't exist
CREATE TABLE IF NOT EXISTS country (
id INT unsigned auto_increment primary key,
name VARCHAR(64)
);
// Insert data into the table if @table_exists > 0
INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands') WHERE 0 < @table_exists;