我可以同時向 MySQL 表中插入兩行或多行嗎?
是的,我們可以一次向表中插入兩行或多行。以下是語法 −
insert into yourTableName(yourColumnName1,yourColumnName2) values(yourValue1,yourValue2),(yourValue1,yourValue2),.........N;
我們先建立一個表 −
mysql> create table DemoTable811( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAge int ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令向表中插入一些記錄 −
mysql> insert into DemoTable811(StudentName,StudentAge) values('Chris',21),('Robert',22),('David',20),('Bob',19),('Carol',23); Query OK, 5 rows affected (0.14 sec) Records: 5 Duplicates: 0 Warnings: 0
使用 select 語句從表中顯示所有記錄 −
mysql> select *from DemoTable811;
這會產生以下輸出 −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 21 | | 2 | Robert | 22 | | 3 | David | 20 | | 4 | Bob | 19 | | 5 | Carol | 23 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
廣告