创建一个表格(如果还不存在的话),并在同一次查询中在 MySQL 中插入一条记录
为此使用 CREATE TABLE IF NOT EXISTS,如下面的语法所示 −
create table if not exists yourTableName ( yourColumnName1 dataType, yourColumnName2 dataType, yourColumnName3 dataType, . . N ) as select yourValue1 as yourColumnName1 , yourValue2 as yourColumnName2 , yourValue3 as yourColumnName3,.............................N;
让我们先创建一个表格,如果表格不存在的话就插入值 −
mysql> create table if not exists DemoTable ( id int, FirstName varchar(20), LastName varchar(20) ) as select 100 as id, 'John' as FirstName , 'Smith' as LastName; Query OK, 1 row affected (0.65 sec) Records: 1 Duplicates: 0 Warnings: 0
现在让我们显示记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+------+-----------+----------+ | id | FirstName | LastName | +------+-----------+----------+ | 100 | John | Smith | +------+-----------+----------+ 1 row in set (0.00 sec)
广告