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; 

 

發佈了331 篇原創文章 · 獲贊 142 · 訪問量 72萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章