运行 全部成功或全部失败的多个查询
Run multiple queries where all succeed or all fail
我需要将 2 行更新为 2 个不同的 table,然后将新行插入到另一个不同的 table 中,必须这样做,这样它们要么全部成功,要么全部失败。 (我认为这个词是原子的?)。我进行了一些搜索,但似乎只能找到与对一个 table.
进行多次插入或更新相关的问题
以下是我需要 运行:
的 3 个查询
UPDATE submission SET status='a' WHERE idgreg = 119 AND status='p' AND userid = 126;
UPDATE greg SET iscomplete=1 WHERE idgreg = 119;
INSERT INTO completion (idperson, idgreg, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idgreg, s.datesubmitted, s.idsubmission, COALESCE(s.youtubeid, s.contentid) AS mediaidid, g.description, g.privatestatus,
g.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN greg g on s.idgreg = g.idgreg
INNER JOIN contribution c on s.idgreg = c.idgreg
WHERE s.idsubmission = 36 AND c.ispaid = 1;
您应该将查询包装在 MySQL 事务 中,以确保它们以原子方式执行。但首先,声明错误处理程序,它将 ROLLBACK
在发生错误时进行所有更改:
CREATE PROCEDURE runYourQueries()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
START TRANSACTION;
UPDATE submission SET status='a' WHERE idgreg = 119 AND status='p' AND userid = 126;
UPDATE greg SET iscomplete=1 WHERE idgreg = 119;
INSERT INTO completion (idperson, idgreg, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idgreg, s.datesubmitted, s.idsubmission,
COALESCE(s.youtubeid, s.contentid) AS mediaidid, g.description, g.privatestatus,
g.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN greg g on s.idgreg = g.idgreg
INNER JOIN contribution c on s.idgreg = c.idgreg
WHERE s.idsubmission = 36 AND c.ispaid = 1;
COMMIT;
END
我需要将 2 行更新为 2 个不同的 table,然后将新行插入到另一个不同的 table 中,必须这样做,这样它们要么全部成功,要么全部失败。 (我认为这个词是原子的?)。我进行了一些搜索,但似乎只能找到与对一个 table.
进行多次插入或更新相关的问题以下是我需要 运行:
的 3 个查询UPDATE submission SET status='a' WHERE idgreg = 119 AND status='p' AND userid = 126;
UPDATE greg SET iscomplete=1 WHERE idgreg = 119;
INSERT INTO completion (idperson, idgreg, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idgreg, s.datesubmitted, s.idsubmission, COALESCE(s.youtubeid, s.contentid) AS mediaidid, g.description, g.privatestatus,
g.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN greg g on s.idgreg = g.idgreg
INNER JOIN contribution c on s.idgreg = c.idgreg
WHERE s.idsubmission = 36 AND c.ispaid = 1;
您应该将查询包装在 MySQL 事务 中,以确保它们以原子方式执行。但首先,声明错误处理程序,它将 ROLLBACK
在发生错误时进行所有更改:
CREATE PROCEDURE runYourQueries()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
START TRANSACTION;
UPDATE submission SET status='a' WHERE idgreg = 119 AND status='p' AND userid = 126;
UPDATE greg SET iscomplete=1 WHERE idgreg = 119;
INSERT INTO completion (idperson, idgreg, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idgreg, s.datesubmitted, s.idsubmission,
COALESCE(s.youtubeid, s.contentid) AS mediaidid, g.description, g.privatestatus,
g.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN greg g on s.idgreg = g.idgreg
INNER JOIN contribution c on s.idgreg = c.idgreg
WHERE s.idsubmission = 36 AND c.ispaid = 1;
COMMIT;
END