MySql定义变量,应用范例

1.定义用户变量(或者赋值)

set @变量名=值

-- 创建自定义用户变量,当当前的用断开连接的时候就会消失
set @value=1;

 

set @pid=1162;
set @smart_id=1356;
select * from tb_iot where corp_id=115 and pid=@pid and properties like CONCAT('%',@smart_id,'%');

 

应用范例:复制一个大棚后,增加修改数据。

#复制大棚#2,改下面的参数。

#1更新网关ID 更新一个棚所有网关 
set @name='大棚#6';
set @pidPeng=1627;   #大棚id 下的所有  
set @pidAllSmart=1628;     #所有智能设备 下的所有 
set @NewgatewayId=20190027; 
#以上参数要改
set @OldgatewayId=20190020; #原    复制大棚#2的数据    
update tb_iot set properties=REPLACE(properties,CONCAT('"gatewayId":"',@OldgatewayId,'"'),CONCAT('"gatewayId":"',@NewgatewayId,'"')) where corp_id=115 and name=@name;
update tb_iot set properties=REPLACE(properties,CONCAT('"gatewayId":"',@OldgatewayId,'"'),CONCAT('"gatewayId":"',@NewgatewayId,'"')) where corp_id=115 and pid in (@pidPeng,@pidAllSmart); #大棚id 和 #所有智能设备





#2更新传感器的smartid
#set @pidPeng=1539;  #大棚id 下的所有  同上
set @Newsmart_id=1629; #新电磁阀
#以上参数要改

set @Oldsmart_id=1442; #原电磁阀 复制大棚#2的数据
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 卷被
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 土壤温湿度#3
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  

set @Newsmart_id=@Newsmart_id+1; #新 
set @Oldsmart_id=@Oldsmart_id+1; #原
update tb_iot set smart_id=@Newsmart_id ,properties=REPLACE(properties,CONCAT('"smartId":"',@Oldsmart_id,'"'),CONCAT('"smartId":"',@Newsmart_id,'"')) where corp_id=115 and pid=@pidPeng  and properties like CONCAT('%"smartId":"',@Oldsmart_id,'"%');  


#1442	电磁阀
#1443	卷被
#1444	土壤温湿度#3
#1445	放风机
#1446	百叶窗#1
#1447	土壤PH值#1
#1448	土壤温湿度#1
#1449	百叶窗#2
#1450	土壤温湿度#2
#1451	百叶窗#3
#1452	土壤PH值#2
#1453	土壤PH值#3

#select * from tb_iot where corp_id=115 and pid=@pidPeng; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值