
您可以使用以下语法组合插入、值和选择语句
insert into yourFirstTableName(yourColumnName1,yourColumnName2,.......N) select yourColumnName1,yourColumnName2,.......N from yourSecondTableName where yourCondition;
为了理解上述语法,让我们创建两个表,其中第一个表将从第二个表获取记录。
让我们创建第一个没有任何记录的表。创建表的查询如下
mysql> create table CombiningInsertValuesSelect -> ( -> EmployeeId varchar(10), -> EmployeeName varchar(100), -> EmployeeAge int -> ); Query OK, 0 rows affected (6.95 sec)
现在您可以创建包含一些记录的第二个表。创建表的查询如下
mysql> create table getAllValues -> ( -> Id varchar(100), -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (1.12 sec)
使用插入命令在第二个表中插入名为“getAllValues”的记录。查询如下
mysql> insert into getAllValues values('EMP-1','John',26);
Query OK, 1 row affected (0.86 sec)
mysql> insert into getAllValues values('EMP-2','Carol',22);
Query OK, 1 row affected (0.36 sec)
mysql> insert into getAllValues values('EMP-3','Sam',24);
Query OK, 1 row affected (0.28 sec)
mysql> insert into getAllValues values('EMP-4','David',27);
Query OK, 1 row affected (0.25 sec)
mysql> insert into getAllValues
values('EMP-5','Bob',21);
Query OK, 1 row affected (0.75 sec)现在您可以使用 select 语句显示表中的所有记录。查询如下
mysql> select *from getAllValues;
以下是输出
+-------+-------+------+ | Id | Name | Age | +-------+-------+------+ | EMP-1 | John | 26 | | EMP-2 | Carol | 22 | | EMP-3 | Sam | 24 | | EMP-4 | David | 27 | | EMP-5 | Bob | 21 | +-------+-------+------+ 5 rows in set (0.00 sec)
这里是MySQL中insert、values和select的使用。查询如下
mysql> insert into CombiningInsertValuesSelect(EmployeeId,EmployeeName,EmployeeAge) -> select Id,Name,Age from getAllValues where Id='EMP-4'; Query OK, 1 row affected (0.23 sec) Records: 1 Duplicates: 0 Warnings: 0
现在使用 select 语句检查记录是否存在于表中。查询如下
mysql> select *from CombiningInsertValuesSelect;
以下是输出
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | EMP-4 | David | 27 | +------------+--------------+-------------+ 1 row in set (0.00 sec)
以上就是在 MySQL 中组合 INSERT、VALUES 和 SELECT的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号