从另一个表插入多行,但插入的记录应该是不同的
为此,可以将DISTINCT与INSERTINTOSELECT语句一起使用。让我们首先创建一个表-
mysql> create table DemoTable1 ( Value int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1 values(50); mysql> insert into DemoTable1 values(10); mysql> insert into DemoTable1 values(10); mysql> insert into DemoTable1 values(60); mysql> insert into DemoTable1 values(50); mysql> insert into DemoTable1 values(70); mysql> insert into DemoTable1 values(50);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable1;
这将产生以下输出-
+-------+ | Value | +-------+ | 50 | | 10 | | 10 | | 60 | | 50 | | 70 | | 50 | +-------+ 7 rows in set (0.00 sec)
以下是创建第二个表的查询。
mysql> create table DemoTable2 ( Marks int );
以下是从另一个表插入多行的查询。插入的记录应该是不同的-
mysql> insert into DemoTable2(Marks) select distinct Value from DemoTable1; Records: 4 Duplicates: 0 Warnings: 0
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2;
这将产生以下输出-
+-------+ | Marks | +-------+ | 50 | | 10 | | 60 | | 70 | +-------+ 4 rows in set (0.00 sec)
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短