我创建了一个mysql过程,但是如果正确执行sql查询,它没有设置值paid = 1:
PROCEDURE `PaidUser`(in IpId varchar(45),in UId int(20))
BEGIN
if(select count(*) as count from users u inner join subscription s on u.ID=s.usersID inner join item i on i.id=s.itemID where i.item_typeID !=6 and u.ID = UId having count >0)
then
update location_byhits set Paid=1 where location_byhits.IpAddress =IpId;
END if;
end
答案 0 :(得分:1)
您从存储过程开始就缺少CREATE
,通过添加此功能,我可以让您的程序在SQL Fiddle 上运行(注意第4个结果集有一行付费更新)
为了测试我创建了以下数据:
CREATE TABLE location_byhits (Paid INT, IpAddress INT);
CREATE TABLE Users (ID INT);
CREATE TABLE Subscription(ItemID INT, UsersID INT);
CREATE TABLE Item (ID INT, Item_TypeID INT);
//
INSERT location_byhits VALUES (0, '192.168.0.0'), (0, '192.168.0.1');
INSERT Users VALUES (1), (2);
INSERT Subscription (1, 1), (2, 2);
INSERT Item (1, 1), (2, 6);
然后用各种参数调用该程序4次。
CALL PaidUser ('192.168.0.2', 1);
CALL PaidUser ('192.168.0.0', 3);
CALL PaidUser ('192.168.0.1', 2);
CALL PaidUser ('192.168.0.1', 1);
我倾向于按如下方式重写您的存储过程:
CREATE PROCEDURE `PaidUser`(in IpId VARCHAR(45),in UId INT(20))
BEGIN
UPDATE location_byhits
SET Paid = 1
WHERE location_byhits.IpAddress = IpId
AND EXISTS
( SELECT 1
FROM Users u
INNER JOIN Subscription s
ON u.ID = s.usersID
INNER JOIN Item i
ON i.ID = s.ItemID
WHERE i.Item_TypeID != 6
AND u.ID = UId
);
END
如果在更新之前进行检查,虽然不太可能,但在启动检查和执行更新之间,数据是由另一个会话更改的并非不可能。这可能会导致意外行为。如果您在一个语句中执行所有操作,则可以避免并发问题。
<强> Example on SQL Fiddle 强>