我有一张桌子,存储友谊,如:
Friend : f_id , f_userid1 , f_userid2 , f_status .
如果用户a是用户b的朋友,我只插入一行:
f_userid1 = a , f_userid2 = b
我想检查一下我是否有这个友谊行,将状态更改为零,我已经为它创建了一个程序:
BEGIN
DECLARE fid INT(15);
-- check for first one
SELECT f_id INTO fid FROM f WHERE
f_userid1 = @userid1
AND f_userid2 = @userid2;
SET oup = 1;
IF(fid = 0) THEN
SET oup = 2;
-- check for SECOUND one
SELECT f_id INTO fid FROM f WHERE f_userid2 = @userid1 AND f_userid1 = @userid2;
END IF;
-- if statment
IF(fid > 0) THEN
SET oup = 3;
-- we need to update the exiting request
UPDATE f SET f_userid1 = @userid1 , f_userid2 = @userid2 , f_status = 0 WHERE f_id = @fid ;
END IF;
END
我之前有一排
f_id = 1 , f_userid1 = a . f_userid2 = b , f_status = 1;
它成功运行,但没有行受到影响!
我添加了输出,它只返回1;
我的问题在哪里?
答案 0 :(得分:0)
为什么不把它写在一个查询中?
UPDATE f
SET f_status = 0
WHERE ( f_userid1 = @userid1 AND f_userid2 = @userid2 )
OR ( f_userid1 = @userid2 AND f_userid2 = @userid1 );