[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>1.定义用户变量(或者赋值)
set @变量名=值
-- 创建自定义用户变量,当当前的用断开连接的时候就会消失 set @value=1;
<>2.查询用户变量值
select @变量
-- 查询定义的数据 select @value;
<>3.在当前的存储过程和函数中使用变量
* 定义变量:
declare 变量名[,变量名2...] 变量类型 [default 默认值]
* 赋值变量:
set 变量名1=变量值1(或者表达式)[ ,变量名2=变量值2(或者表达式)]
* 使用变量:
select 列名[,列名...] into 变量名1[,变量名二...]
<>4.创建更新数据的存储过程
创建表mytest
-- 创建更新的存储过程 DELIMITER // CREATE PROCEDURE updateMyTest(uid INT,newMoney DOUBLE
) BEGIN DECLARE isexists INT DEFAULT 0; -- 定义变量必须在当前的begin中 SELECT COUNT(1) INTO
isexistsFROM mytest WHERE id=uid ; -- 使用查询的(into)方式赋值 IF isexists<>0 THEN --
第一个条件IF newMoney>0 THEN -- 第二个条件 UPDATE mytest SET money=newMoney WHERE id=uid;
SET isexists=1; -- 使用set方式为变量赋值 ELSE SET isexists=-1; END IF; -- 结束第二个条件 END IF;
-- 结束第一个条件 SELECT (CASE isexists WHEN -1 THEN '执行失败' WHEN 0 THEN 'id不存在' ELSE
'执行成功' END)AS '执行结果' ;-- 使用case when then else end 的switch条件 END // DELIMITER ;
测试:CALL updateMyTest(1,10000);
<>5.创建输入成绩返回结果的函数
-- 根据输入的成绩获得成绩的结果 DELIMITER // CREATE FUNCTION getScoreResult(score INT)
RETURNS CHAR(2) BEGIN DECLARE result CHAR(2) DEFAULT ''; IF score>90 THEN SET
result='优'; ELSEIF score>80 THEN SET result='良'; ELSEIF score>70 THEN SET result
='一般'; ELSE SET result='差'; END IF; --结束if条件 RETURN result; END // DELIMITER ;
测试:SELECT getScoreResult(50);
<>6.总结
1.定义用户变量使用set @变量,该变量随着用户连接断开就直接消失
2.赋值都是使用set 变量名=值;一定要加分号
3.使用case 变量 when 值 then … else end ,其中else相当于switch中的default ,一定要加上end结束case语法
4.使用if-elseif-else或者if-else的时候需要在末尾加上end if用来结束if的语法
以上纯属个人见解,如有问题请联系本人!