zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql insert or replace_dbinsert

mysql or INSERT replace
2023-06-13 09:13:45 时间

大家好,又见面了,我是你们的朋友全栈君。

通常情况下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属性的设置问题:

  1. separator设置为”,”分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,…
  2. 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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。