SQL - Wie man eine bedingte EINFÜGEN
Mit nur MySQL, ich bin zu sehen, wenn es möglich ist, ausführen einer insert-Anweisung NUR, wenn die Tabelle neu ist. Habe ich erfolgreich erstellt, eine user-variable, um zu sehen, wenn die Tabelle vorhanden ist. Das problem ist, dass Sie nicht verwenden können, "WO", zusammen mit einem insert-Anweisung. Irgendwelche Ideen auf, wie dies funktioniert?
// 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;
Du musst angemeldet sein, um einen Kommentar abzugeben.
Verwenden Sie eine if-Anweisung anstelle der where-Klausel:
http://dev.mysql.com/doc/refman/5.0/en/if-statement.html