概述
定义标量用子查询的概念,专门用一个子查询定义变量用@i:=赋值,用@i=比较,一般用于行号记录mysql没有rownum
(select @i:=0,@k:='') idx
@i:=case when @k=store_code then @i+1 else 1 end as row_no,@k:=store_code
@k,开始第一行用自定义的初始值,后续用每行新赋值的
<select id="getAllStorePriceTireInfo"
resultType="com.mcd.cn.rdc.pcm.product.business.vo.store.StorePriceTierVO">
select s.`code`, s.price_tier_code as priceTierCode, mds.price_tier_code as mdsPriceTierCode, mcf.price_tier_code as mcCafePriceTierCode
from t_store s
LEFT OUTER JOIN (
select `code` as beCode, price_tier_code, store_code,@i:=case when @k=store_code then @i+1 else 1 end as row_no,@k:=store_code
from t_store_be,(select @i:=0,@k:='') idx
where type=2 and is_effective=1 and price_tier_code is not null
order by store_code,updated_date DESC,`code` DESC
) mds on (mds.store_code=s.`code` and mds.row_no=1)
LEFT OUTER JOIN (
select `code` as beCode, price_tier_code, store_code,@i:=case when @k=store_code then @i+1 else 1 end as row_no,@k:=store_code
from t_store_be,(select @i:=0,@k:='') idx
where type=3 and is_effective=1 and price_tier_code is not null
order by store_code,updated_date DESC,`code` DESC
) mcf on (mcf.store_code=s.`code` and mcf.row_no=1)
where s.is_effective=1
</select>
最后
以上就是甜蜜大门为你收集整理的在sql中定义变量,动态运算、赋值的全部内容,希望文章能够帮你解决在sql中定义变量,动态运算、赋值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复