mysql 触发器 获取查询结果,mySql中触发器的有关问题:当查询语句查询结果为空时出错...

mysql中触发器的问题:当查询语句查询结果为空时出错。

mySql中触发器的问题:当查询语句查询结果为空时出错。错误信息为:

No   data   -   zero   rows   fetched   ,   selected,   or   proecssed.

有人遇到过吗?

我的触发器如下:

CREATE   TRIGGER   `temp_after_ins_tr`   AFTER   INSERT   ON   `temp`

FOR   EACH   ROW

BEGIN

set   @countNumber   =   0;

set   @value1   =   '0 ';

set   @insertDaytime   =   ' ';

Select   1,daytime,(NEW.value1)   into   @countNumber,@insertDaytime,@value1

From   temp

where   substationid=NEW.substationid   and   switchid=NEW.switchid

and   sanchu=0   and   daytime   <   NEW.daytime

order   by   daytime   DESC

limit   1;

if   @countNumber=1   then

insert   into   temp2(datetimes,substationid,switchid,value1)

values

(@insertDaytime,NEW.substationid,NEW.switchid,@value1);

end   IF;

END;

结果在执行

Select   1,daytime,(NEW.value1)   into   @countNumber,@insertDaytime,@value1

From   temp

where   substationid=NEW.substationid   and   switchid=NEW.switchid

and   sanchu=0   and   daytime   <   NEW.daytime

order   by   daytime   DESC

limit   1;

这个查询时,如果查询不到数据,则出错了。

请问我要怎么做?

------解决方案--------------------

try:

CREATE TRIGGER `temp_after_ins_tr` AFTER INSERT ON `temp`

FOR EACH ROW

BEGIN

set @countNumber = 0;

set @value1 = '0 ';

set @insertDaytime = ' ';

Select 1,daytime,(NEW.value1) into @countNumber,@insertDaytime,@value1

From temp

where substationid=NEW.substationid and switchid=NEW.switchid

and sanchu=0 and daytime < NEW.daytime

order by daytime DESC

limit 1;

insert into temp2(datetimes,substationid,switchid,value1)

select @insertDaytime,NEW.substationid,NEW.switchid,@value1

where ifnull(@countNumber,-1)=1;

END;

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值