在群里遇到有人问了个问题:其表结构如下:
[img]http://dl.iteye.com/upload/attachment/0064/2472/638cab42-4499-3249-9f1a-4aecefd73340.jpg[/img]
他想要用SQL 如何更新 站号 列各出现的次数到 频次 列。(注:站号对应 name,频次对应 no)
最后用游标实现了他想的功能:
[img]http://dl.iteye.com/upload/attachment/0064/2472/638cab42-4499-3249-9f1a-4aecefd73340.jpg[/img]
他想要用SQL 如何更新 站号 列各出现的次数到 频次 列。(注:站号对应 name,频次对应 no)
最后用游标实现了他想的功能:
create procedure pro_hzcount()
begin
declare cur_no,y int;
declare cur_name varchar(20);
declare cursor_1 cursor for SELECT name,count(name) FROM t1 group by name;
declare continue handler for not found
set y=1;
open cursor_1;
repeat
fetch cursor_1 into cur_name,cur_no;
update t1 set no=cur_no where name=cur_name;
until y=1 end repeat;
close cursor_1;
end;
:wink: