gpt4 book ai didi

mysql - 根据触发器中的子查询计算值

转载 作者:行者123 更新时间:2023-11-29 23:16:18 25 4
gpt4 key购买 nike

我想在 MySql 中创建一个触发器以在表 A 中插入一些值,如果记录已存在,我想重新计算其中一个字段,但我不断收到语法错误:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'P1

附近使用的正确语法

内连接(第 6 行选择 SUM(ATS) AS TOTAGAINST, TOURNID, HOMETEAM, ROUNDID'

这是我的代码(为了调试而删除):

DROP TRIGGER IF EXISTS `trig_matches_insert`;
DELIMITER //
CREATE TRIGGER `trig_matches_insert` AFTER INSERT ON `matches`
FOR EACH ROW
BEGIN
INSERT INTO positions (TEAMID, SC_AGAINST, SC_FOR, ROUNDID, TOURNID) values
(new.HOMETEAM, new.ATS, new.HTS, new.ROUNDID, new.TOURNID)
ON DUPLICATE KEY
UPDATE positions P1
INNER JOIN
(
SELECT SUM(ATS) AS TOTAGAINST, TOURNID, HOMETEAM, ROUNDID FROM matches GROUP BY
TOURNID,HOMETEAM, ROUNDID
) P2 ON P2.HOMETEAM = P1.TEAMID AND P2.TOURNID = P1.TOURNID AND P2.ROUNDID = P1.ROUNDID
SET P1.SC_AGAINST = P2.TOTAGAINST WHERE P1.ID = 10;
END
//
DELIMITER ;

我不知道这里出了什么问题..当我独立运行更新查询时,它运行良好

这里有一些支持的sql:

CREATE TABLE IF NOT EXISTS `matches` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TOURNID` int(11) NOT NULL,
`HOMETEAM` int(11) NOT NULL,
`AWAYTEAM` int(11) NOT NULL,
`HTS` decimal(10,0) NOT NULL,
`ATS` decimal(10,0) NOT NULL,
`FIELD` text NOT NULL,
`TIME` datetime NOT NULL,
`ROUNDID` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `TOURNID` (`TOURNID`),
KEY `HOMETEAM` (`HOMETEAM`),
KEY `AWAYTEAM` (`AWAYTEAM`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;

INSERT INTO `matches` (`ID`, `TOURNID`, `HOMETEAM`, `AWAYTEAM`, `HTS`, `ATS`, `FIELD`, `TIME`, `ROUNDID`) VALUES
(17, 1, 30, 31, '2', '3', '', '2015-01-04 00:00:00', 1),
(18, 1, 30, 3, '2', '4', '', '2015-01-04 00:00:00', 1),
(19, 1, 30, 4, '3', '5', '', '2015-01-04 00:00:00', 1);

CREATE TABLE IF NOT EXISTS `positions` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TOURNID` int(11) NOT NULL,
`TEAMID` int(11) NOT NULL,
`ROUNDID` int(11) NOT NULL,
`SC_FOR` decimal(10,0) NOT NULL,
`SC_AGAINST` decimal(10,0) NOT NULL,
`POULEID` int(11) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `TOURNID_2` (`TOURNID`,`TEAMID`,`ROUNDID`),
KEY `TOURNID` (`TOURNID`,`TEAMID`),
KEY `TEAMID` (`TEAMID`),
KEY `ROUND` (`ROUNDID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

INSERT INTO `positions` (`ID`, `TOURNID`, `TEAMID`, `ROUNDID`, `SC_FOR`, `SC_AGAINST`, `POULEID`) VALUES
(10, 1, 30, 1, '2', '12', 0);

提前致谢

迈克

最佳答案

我想我已经找到了它,看起来在 ON DUPLICATE KEY 之后,位置表已经获得了范围,不需要从头开始构建和更新语句,似乎快速选择就可以了:

DROP TRIGGER IF EXISTS `matches_after_ins_trig`;
DELIMITER //
CREATE TRIGGER `matches_after_ins_trig` AFTER INSERT ON `matches`
FOR EACH ROW
BEGIN
INSERT INTO positions (TEAMID, SC_AGAINST, SC_FOR, ROUNDID, TOURNID) values (new.HOMETEAM, new.ATS, new.HTS, new.ROUNDID, new.TOURNID)
ON DUPLICATE KEY UPDATE SC_AGAINST = (SELECT SUM(ATS) FROM matches WHERE
matches.HOMETEAM = new.HOMETEAM AND matches.TOURNID = new.TOURNID AND
matches.ROUNDID = new.ROUNDID GROUP BY TOURNID,HOMETEAM, ROUNDID);
END

关于mysql - 根据触发器中的子查询计算值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com