在单个MySQL查询中使用两个SELECT语句将值从第一张表插入第二张表
若要使用两个SELECT语句将值从第一个表插入另一个表,请使用SUBQUERY。这将允许您仅使用单个MySQL查询来获取第二个表中的结果。让我们首先创建一个表-
mysql> create table DemoTable1 ( Name varchar(100), Score int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1 values('Chris',45);
mysql> insert into DemoTable1 values('Bob',78);
mysql> insert into DemoTable1 values('David',98);
mysql> insert into DemoTable1 values('Carol',89);使用select语句显示表中的所有记录-
mysql> select *from DemoTable1;
这将产生以下输出-
+-------+-------+ | Name | Score | +-------+-------+ | Chris | 45 | | Bob | 78 | | David | 98 | | Carol | 89 | +-------+-------+ 4 rows in set (0.00 sec)
以下是创建第二个表的查询。
mysql> create table DemoTable2 ( StudentName varchar(100), StudentScore int );
现在让我们编写一个MySQL查询,使用两个SELECT语句将值从第一张表插入第二张表-
mysql> insert into DemoTable2(StudentName,StudentScore) values((select Name from DemoTable1 where Score=98),(select Score from DemoTable1 where Name='David'));
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2; +-------------+--------------+ | StudentName | StudentScore | +-------------+--------------+ | David | 98 | +-------------+--------------+ 1 row in set (0.00 sec)
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短