在单个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 妈妈生日祝福语简短励志