关联插入Mysql
// codeType传入值,@rowno 获取每行,可以去掉,codeNum定义的值
insert into sys_upload_file (name, code)
select b.name,concat(#{codeType},@rowno:=@rowno+1) code
from dat_goods a, gds_goodscontrast b ,(select @rowno:=#{codeNum}) t
where a.id = b.goodsId
关联更新Mysql
update temp a INNER JOIN (
select id,(case when inventoryQuantity is null then 0 else 0-inventoryQuantity end),
lostOrIncomeQuantity from temp2 ) b on a.id = b.id
set
a.lostOrIncomeQuantity = b.lostOrIncomeQuantity
mysql 字符串处理相关截取
SELECT SUBSTRING_INDEX('aMc','M',-1)
SELECT REVERSE(LEFT(REVERSE('test.htm'),INSTR(REVERSE('test.htm'),'.')))
SELECT LOCATE('M', 'LF1212 石基(黑砂) 1000×1000mm')
SELECT REVERSE('abc')
SELECT substring( REVERSE('abc'),1,LOCATE(' ', REVERSE('abc)'))-1),REVERSE('abc')
(case
when ifnull(g.smallUnit,'')<>'' && ifnull(d.smallUnit,'')<>'' && g.smallUnit=d.smallUnit then g.produce
else 0 end)
Mybatis—动态查询(choose,when,otherwise)
choose (when, otherwise)标签
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>