小白发帖如何写存储过程
小白发帖求助怎么写存储过程
我看了教学,发现没看到我需要的东西
比如
CREATE PROCEDURE xx()
BEGIN
/* 我想实现的是,2个表,table1 table2, 把table1里没有,但是table2里有的记录插入到table1里,字段名字不一样。*/
DECLARE done INT;
DECLARE cur_1 CURSOR FOR SELECT id ,email FROM table2;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = 1;
OPEN cur_1
/*这里就不会写了,不会对游标操作,第n行第x字段,还有判断另外一个表有没有相同的id email记录*/
CLOSE cur_1;
END;
------解决方案--------------------
看MYSQL手册中的例子。
------解决方案--------------------
试一下不就知道了?
------解决方案--------------------
完全没必要用游标
create procedure sp_test()
begin
insert into tb1(a,b)
select a,b from tb2 A where not exists (select 1 from tb1 where A.a=a and A.b=b)
end
------解决方案--------------------
我想实现的是,2个表,table1 table2, 把table1里没有,但是table2里有的记录插入到table1里,字段名字不一样
insert into t1(f1,f2)
select a.f1,a.f2 from t2 a left join t1 b on a.userid = b.uid where b.uid is null
我看了教学,发现没看到我需要的东西
比如
CREATE PROCEDURE xx()
BEGIN
/* 我想实现的是,2个表,table1 table2, 把table1里没有,但是table2里有的记录插入到table1里,字段名字不一样。*/
DECLARE done INT;
DECLARE cur_1 CURSOR FOR SELECT id ,email FROM table2;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = 1;
OPEN cur_1
/*这里就不会写了,不会对游标操作,第n行第x字段,还有判断另外一个表有没有相同的id email记录*/
CLOSE cur_1;
END;
------解决方案--------------------
看MYSQL手册中的例子。
------解决方案--------------------
试一下不就知道了?
------解决方案--------------------
完全没必要用游标
create procedure sp_test()
begin
insert into tb1(a,b)
select a,b from tb2 A where not exists (select 1 from tb1 where A.a=a and A.b=b)
end
------解决方案--------------------
我想实现的是,2个表,table1 table2, 把table1里没有,但是table2里有的记录插入到table1里,字段名字不一样
insert into t1(f1,f2)
select a.f1,a.f2 from t2 a left join t1 b on a.userid = b.uid where b.uid is null