mysql insert or replace_dbinsert
大家好,又见面了,我是你们的朋友全栈君。
通常情况下insert语句的写法为 insert into tablename values (a,b);
区别之处:
1oracle中使用如下语句
1.1方式一
该方式特点是能插如值是固定的多条数据 insert all into test01 values(1,’a’) into test01 values(2,’b’) select 1 from dual; –这一行不能去掉
1.2方式二
该方式特点是:能插入一些值不是固定的多条数据.可以吧其他表中的数据批量保存到这张表中来 insert into test01 (id,line1)
select id,line1 from test02;
1.3方式三
放在begin end里面如下:
begin insert into test01 (id,line1) values (01,’line01′); insert into test01 (id,line1) values (01,’line01′); insert into test01 (id,line1) values (01,’line01′); insert into test01 (id,line1) values (01,’line01′); end;
这个可以在Mybatis的sql中优化为
<insert id=”insertRoleInfoList” parameterType=”java.util.Map”> <foreach collection=”roleInfoList” item=”userRoleInfo” index=”index” open=”begin” close=”;end;” separator=”;”> insert into base_role_demo (roleid,rolecode,roledesc,remark,recordercode,recorderdesc,usecorpcode,usecorpdesc) values(ROLEDEMO_CORP_SEQ.Nextval,#{userRoleInfo.rolecode},#{userRoleInfo.roledesc},#{userRoleInfo.remark}, #{recordercode},#{recorderdesc},#{userRoleInfo.usecorpcode},#{userRoleInfo.usecorpdesc}) </foreach> </insert>
2mysql使用如下语句
insert into test01 (id,line1) values (01,’line01′),(02,’line02′);
这种连写的方式可以同时添加多条
接下来说一说关于foreach 语句的区别
主要不同点在于foreach标签内separator属性的设置问题:
- separator设置为”,”分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,…
- separator设置为”union all”分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) union all (v4,v5,v6) union all…
oracle中:
<insert id=”inserData” parameterType=”com.test.aaa.Bac”> insert into table_name (name, adress, age) values <foreach collection=”list” item=”item” index=”index” separator=”union all”> (select #{item.name}, #{item.adress}, #{item.age} from dual ) </foreach> </insert>
MySQL中:
<insert id=”inserData” parameterType=”com.test.aaa.Bac”> insert into table_name (name, adress, age) values <foreach collection=”list” item=”item” index=”index” separator=”,”> ( #{item.name}, #{item.adress}, #{item.age} ) </foreach> </insert>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
相关文章
- mysql怎么查询上一条记录_MySQL中查询已知记录的上一条和下一条记录「建议收藏」
- MySQL insert or update sql
- 【MySQL高级】MySql中常用工具及Mysql 日志
- MySQL中快速插入大量数据的方法(mysql批量insert数据)
- 如何利用MySQL实现多数据库的同步(mysql同步不同数据库)
- Python3:Django连接Mysql数据库时出错,’Did you install mysqlclient or MySQL-python’详解编程语言
- MySQL公网访问:掌握这些知识点(mysql公网访问)
- MySQL统计连接数的实现方式(mysql统计连接数)
- MySQL认证:多少钱?(mysql认证多少钱)
- MySQL中实现多个字段OR运算的方法(mysql多个字段or)
- 性能MySQL索引调优:提升性能的关键(mysql索引or)
- 语句使用MySQL中的IN和OR语句(mysql中in和or)
- MySQL免密码尝试:一键登录/登出方法。(mysql免密码登录)
- MySQL远程使用:掌握基础知识,远程操作Mysql数据库。(mysql远程使用)
- 简单易学:2003如何安装MySQL(2003安装mysql)
- MySQL的MVCC技术:实现数据库性能的优化篇(mvcc mysql)
- MySQL中的AND和OR运算符(mysql中且或者符号)
- MySQL中OR函数实现条件搜索(mysql中or函数)
- 管理c语言与MySQL联合实现项目管理(c mysql 项目)
- 使用MySQL实现两表ID自增,优化数据存储与管理(mysql两表ID自增)
- MySQL创建表示例快速掌握MYSQL基础操作(mysql中创建表的例子)
- MySQL下载及安装教程快速掌握MySql下载及安装方法,更高效地使用MySql数据库(mysql下载了在哪)
- Mysql 数据库丢失别慌来了解一下 MySQL 不见的可能原因及解决办法(mysql不见)
- 速度提升百倍MySQL一天以内的性能优化方法(mysql一天以内)