在单个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 五二零朋友祝福语大全简短
13 新年祝福语独创文字简短
14 老婆生日祝福语简短孩子
15 朋友节最简短祝福语
16 英文简短唯美祝福语大全
17 写生日祝福语简短唯美
18 开业经典祝福语简短