我有一个运行正常的“SELECT CASE”语句查询:
SELECT
(CASE `t`.`is_combined`
WHEN 0
THEN `t`.`topic_id`
ELSE `t`.`is_combined`
END) AS`group_id`,
SUM(`ctt`.`tm_download_status`) AS `is_downloaded`,
COUNT(`t`.`topic_id`) AS `group_topics_cnt`,
(SUM(`ctt`.`tm_download_status`) = COUNT(`t`.`topic_id`)) AS `is_downloaded_group`
FROM (`catalog_topics` `t` LEFT JOIN `catalog_tracker_torrents` `ctt` ON((`ctt`.`topic_id` = `t`.`topic_id`)))
WHERE (`t`.`topic_id` != 0)
GROUP BY (`group_id`)
所以,我想创建一个类似的触发器来更新“交叉”表:
DELIMITER $$
CREATE TRIGGER `tdg_ins_by_topics` AFTER INSERT ON `catalog_topics` FOR EACH ROW
BEGIN
REPLACE INTO catalog_topics_downloaded_groups(
SELECT (
CASE `t`.`is_combined`
WHEN 0
THEN `t`.`topic_id`
ELSE `t`.`is_combined`
END
) AS `group_id` ,
SUM( `ctt`.`tm_download_status` ) AS `is_downloaded` ,
COUNT( `t`.`topic_id` ) AS `group_topics_cnt` , (
SUM( `ctt`.`tm_download_status` ) = COUNT( `t`.`topic_id` ) ) AS `is_downloaded_group`
FROM `catalog_topics` `t`
LEFT JOIN `catalog_tracker_torrents` `ctt` ON `ctt`.`topic_id` = `t`.`topic_id`
WHERE `t`.`topic_id`
IN (
NEW.`topic_id`
)
GROUP BY `group_id`
)
END ;
$$
但收到错误消息:
"#"1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 14 行的“END”附近使用正确的语法
看起来 MySQL 不理解CASE
in TRIGGER
statement 和CASE
in SELECT
statement 之间的区别。那么,我该如何解决这个问题?
感谢您的回答。