我在商店程序中使用PREPARE
语句,但是在记录插入时花费了很多时间。请建议我替换PREPARE
声明。
SET @COUNT = 0;
SET @countQuery = CONCAT("select count(ufl.id) INTO @COUNT from user_friend_list as ufl where ufl.user_id = ",@USERREGID," AND ufl.friend_user_id = ",FID);
PREPARE STMTC FROM @countQuery;
EXECUTE STMTC;
IF @COUNT = 0 THEN
INSERT INTO user_friend_list(user_id, friend_user_id) VALUES(@USERREGID,FID),(FID,@USERREGID);
END IF;
DEALLOCATE PREPARE STMTC;
答案 0 :(得分:2)
为什么不这样做呢
IF 0 = (SELECT COUNT(*)
FROM user_friend_list
WHERE user_id = @USERREGID
AND friend_user_id = FID) THEN
INSERT INTO user_friend_list(user_id, friend_user_id)
VALUES (@USERREGID,FID), (FID,@USERREGID);
END IF;
这是 SQLFiddle 演示