mysql 在select查询语句中使用临时变量累计求和 ; 相同列值的记录中再根据条件取其中最大或最新一条;mysql经纬度求距离并排序

使用临时变量累计求和

我们用的示例表

idcreate_timecreate_bymaterial_idshare_click
12021-01-20 02:18:2311100share
22021-01-20 02:19:1511200share
32021-01-20 02:20:1011100click
42021-01-20 02:18:0811200share
52021-01-20 02:18:3211100click

码代码

可以直接写在mybatis mapper里面的方式去使用变量

select id, create_time, create_by,  material_id, share_click ,
       @sum := @sum + 1 as rownumber , (@sumScore := @sumScore + @sum) as sum_score ,  # 局部变量赋值 + 运算
       if((@sumScore ) % 2 = 0 , '偶数','非偶数') as 是否偶数  # 局部变量使用
from material_share_click ,
     (select @sum := 0 , @sumScore := 0) as temp__   # 局部变量定义

查询结果

idcreate_timecreate_bymaterial_idshare_clickrownumbersum_score是否偶数
12021-01-20 02:18:2311100share11非偶数
22021-01-20 02:19:1511200share23非偶数
32021-01-20 02:20:1011100click36偶数
42021-01-20 02:18:0811200share410偶数
52021-01-20 02:18:3211100click515非偶数

相同的ID,取最大或者最小的那个 (相同列值的记录中再根据条件取其中最大或最新一条)

示例用的表

idcreate_timecreate_bymaterial_idshare_click
12021-01-20 02:18:2311100share
22021-01-20 02:19:1511200share
32021-01-20 02:20:1011100click
42021-01-20 02:18:0811200share
52021-01-20 02:18:3211100click

码代码

有点儿窗口函数那意思哈

select id, create_time, create_by,  material_id, share_click
from material_share_click
where not exists(
    select *
     from material_share_click bb
    where material_share_click.material_id = bb.material_id
      and material_share_click.create_time < bb.create_time
  )

查询结果

idcreate_timecreate_bymaterial_idshare_click
22021-01-20 02:19:1511200share
32021-01-20 02:20:1011100click

case when

              case 订单状态
                when 100 then '未开始'
                when 200 then if(支付状态= 1, '已支付', '未支付')
                when 300 then
                  case 配送方式
                    when '快递' then
                      if(是否签收= 1, '已收货',
                         if(物流单号 != '' or 物流单号 is null, '未发货', '已发货'))
                    when '自提' then if(issign = 1, '已使用', '未使用')
                    end
                when 400 then '已完成'
                end             as 实际订单状态

经纬度求距离并排序

公式1


SELECT *,
(st_distance(
	point(longitude,latitude),
	point( 121.441345 , 31.176867 ))*111195/1000 
) as juli 
	FROM store
ORDER BY juli ASC

公式2

SELECT *,
	ROUND(6378.138*2*ASIN(SQRT(POW(SIN((${lat} *PI()/180-latitude *PI()/180)/2),2)+COS(${lat} *PI()/180)*COS(latitude *PI()/180)*POW(SIN((${lng} *PI()/180- longitude*PI()/180)/2),2)))*1000
	) as distance
FROM lbsTable
order by distance asc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值