假设您的表名是'table55'
然后使用以下查询创建存储过程
delimiter |
create procedure testProcedure()
BEGIN
declare flag1 int default 0;
DECLARE done INT DEFAULT FALSE;
declare tempid int;
declare tempdate date;
declare tempname,templogin,templogout varchar(50);
DECLARE cur1 CURSOR FOR SELECT * from table55 where `name` like 'arjun' and id=1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
drop table if exists mytemptable;
CREATE TEMPORARY TABLE IF NOT EXISTS mytemptable ( `name` VARCHAR(50) NULL DEFAULT NULL,
`id` INT(11) NULL DEFAULT NULL,
`date` DATE NULL DEFAULT NULL,
`login` VARCHAR(50) NULL DEFAULT NULL,
`logout` VARCHAR(50) NULL DEFAULT NULL);
OPEN cur1;
read_loop: LOOP
FETCH cur1 into tempname,tempid,tempdate,templogin,templogout;
if flag1=0 then
insert into mytemptable(name,id,`date`,login,logout) values (tempname,tempid,tempdate,templogin,templogout);
set flag1=1;
else
insert into mytemptable(name,id,`date`,login,logout) values ('',null,tempdate,templogin,templogout);
end if;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
select * from mytemptable;
END
|
delimiter;
您只需运行上述查询一次。之后,您可以使用以下查询来获得所需的结果
call testProcedure