0
我需要重新命名我的一些MySQL表,从而弄乱我的程序。至少这就是我的想法......我得到一个前所未有的错误。'where子句'中未知的列'discussion.rubric_id'
两个表格很重要:一个称为rubrics
,它有一列来计算与rubric
相关的discussions
。因此,无论何时插入新的discussion
,都会触发procedure
,使计数器增加。
这是两个表(简称):
这是我专栏表
CREATE TABLE IF NOT EXISTS `rubric` (
`id` INT NOT NULL AUTO_INCREMENT ,
`topic_id` INT NOT NULL ,
`name` VARCHAR(50) NULL ,
`description` TEXT NULL ,
`total_discussions` INT NULL DEFAULT 0 , // this is the COUNTER
`latest_activity_id` INT NULL ,
PRIMARY KEY (`id`, `topic_id`) ,
... // just some index and constraint..
ENGINE = InnoDB;
这是我讨论表
CREATE TABLE IF NOT EXISTS `discussion` (
`id` INT NOT NULL AUTO_INCREMENT ,
`rubric_id` INT NOT NULL , // as you can see it has a rubric_id column
`topic_id` INT NOT NULL ,
`latest_activity_id` INT NULL DEFAULT 0 ,
`name` VARCHAR(50) NULL ,
`description` TEXT NULL ,
`total_posts` INT NULL DEFAULT 0 ,
`total_views` INT NULL DEFAULT 0 ,
PRIMARY KEY (`id`, `rubric_id`, `topic_id`) ,
... // just some index and constraint..
ENGINE = InnoDB;
这里是程序应该增加计数器在评鉴表:
DELIMITER $$
CREATE TRIGGER count_discussions AFTER INSERT ON discussion
FOR EACH ROW
BEGIN
UPDATE rubric SET total_discussions = total_discussions + 1 WHERE rubric.id = discussion.rubric_id;
END $$
插入新行的时候,我得到的错误是:
Error code 1054, SQL state 42S22: Unknown column 'discussion.rubric_id' in 'where clause'
所以我猜测它是关于的WHERE子句程序因为我没有其他WHERE子句提到rubric_id co lumn。但正如您在讨论表中看到的那样,该列存在。那么这是一种我不尊重的错字或某种语法吗?
非常感谢那个快速和工作的答案。不知道我可以加入触发器!它现在正在工作,但我仍然想知道为什么它在重命名表之前工作...谢谢! – GameDroids
欢迎您:)! –