数据库插入数据之select into from与insert into select区别详解
<p>可能第一次接触select...into...from...和insert into...select...有很多人都会误解, 从表面上看都是把相关信息查询出来,然后添加到一个表里,其实还远远没有这么简单,接下来,小猪就用最普通的表述给大家介绍一下这两者间的区别。</p>
<p>
步骤/方法</p>
<p>
1、首先,我们来看一下insert into select语句,其语法形式为:insert into table2(field1,field2,...) select value1,value2,... from table1。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/49a272aa0dc186143068d9a4413d5ba0.jpg"></p>
<p>
2、这里的要求就是table2必须已经存在,如果不存在,系统则会提示对象无效。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/81224609ad0b017218c9bb6945490a7f.jpg"></p>
<p>
3、同时因为table2已经存在,所以我们就可以任意的向table2中加入符合列类型的内容,当然也包括常量,也许通过下面的图,你就会发现,在导入常量时候的不寻常现象了。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/3931f5b64589d214c5f359706caadd03.jpg"></p>
<p>
4、接下来说一下select into from 语句,该语法形式为:select vale1, value2 into table2 from table1。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/2e1c087d71e4e149563ee6b0dd7700aa.jpg"></p>
<p>
5、这里要求的是mytable1不存在,因为在插入的时候,系统会自动创建mytable1,如果之前mytable1已经被创建,系统就会提示已经存在表。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/e6551681f3379f0efab3177d6f2ba4f7.jpg"></p>
<p>
6、就是因为这个特性,就会给我们带来很多的方便,首先,我如果只想要mytable2表中的id列的话那我只需要将id列select出来,然后加入到新表中即可。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/b03a703f63780bc5d83c3babc58103a2.jpg"></p>
<p>
7、那如果我想要mytable2中的name列,那我也仅仅是需要把name列select出来,然后添加到新表中即可。</p>
<p>
</p>
<p>
<img title="数据库插入数据之select into from与insert into select区别详解" alt="数据库插入数据之select into from与insert into select区别详解" src="https://zhuji.jb51.net/uploads/img/202305/2764ce3cf1be747b2c5ebe7b2acec547.jpg"></p>
<p>
end</p>
<p>
注意事项</p>
<p>
应用范围由实践者自己找到答案~</p>
頁:
[1]