MySQL 执行提示: Result consisted of more than one row 问题解析

开放功能需要冬天维护一个编号,这个编号是根据区域编号生成的,于是乎就写一个触发器动态维护,更新或者插入时自动判断累计10增长,

代码示例:

drop trigger if EXISTS trig_stationno_insert;
DELIMITER //
CREATE TRIGGER trig_stationno_insert BEFORE INSERT ON ins_station
  FOR EACH ROW BEGIN
			DECLARE n_stno varchar(50);
			-- select CONCAT(citycode,right('1000000'+'8',6)) into n_stno from ins_station where citycode = NEW.citycode;
			select max(ifnull(stno,concat(citycode,right('1000000'+'8',6)))) into n_stno from ins_station where citycode = NEW.citycode;
			set new.stno=n_stno+10;
  END
//
drop trigger if EXISTS trig_stationno_update;
DELIMITER //
CREATE TRIGGER trig_stationno_update BEFORE update ON ins_station
  FOR EACH ROW BEGIN
			DECLARE n_stno varchar(50);
			if (new.stno is null)
			then 
				select max(ifnull(stno,concat(citycode,right('1000000'+'8',6)))) into n_stno from ins_station where citycode = NEW.citycode;
				set new.stno=n_stno+10;
			end if;
  END
//

     其中涉及到几个难点,第一自动补零  因为就算是字符串用加号以后也会转成数值运算,智能通过高位运算在截取,比如你想得到   0005   就得拿10000+5 然后右截取4位。可能这里弄复杂了,不过能实现就好!

在过程中遇到一些问题,比如触发器中不能再使用update语句的问题;

还有一个问题比较郁闷,后面再反应过来,就是题目中的错误:Result consisted of more than one row

估计是一种机制的问题,在给某个变量赋值的时候(select xx into xxValue)只能有一个值,所以最好 保证记录的唯一性,用max sum min函数最好的方式。

这个或许在执行存储过程 或函数 或者触发器都会出的错误!

发布了70 篇原创文章 · 获赞 16 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/ucicno000/article/details/82493428