5

MySQL のみを使用して、テーブルが新しい場合にのみ挿入ステートメントを実行できるかどうかを確認しています。テーブルが存在するかどうかを確認するためのユーザー変数を正常に作成しました。問題は、insert ステートメントと一緒に「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;
4

2 に答える 2

6
IF @TableExists > 0 THEN
   BEGIN
       INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands');
   END
于 2008-11-15T15:25:14.340 に答える
2

where 句の代わりに if ステートメントを使用します。

http://dev.mysql.com/doc/refman/5.0/en/if-statement.html

于 2008-11-15T15:26:04.213 に答える