0

假设我有 2 张桌子:

DROP TABLE IF EXISTS `datasetCalculation`;
DROP TABLE IF EXISTS `dataset`;


CREATE TABLE IF NOT EXISTS `dataset`(
    `datasetID` INT NOT NULL AUTO_INCREMENT,
    `datasetWeekOf` DATE, #date indicates the beginning of a week
    `dataInsertedByID` INT NOT NULL,
    `datasetItem1` INT,
    `datasetItem2` INT,
    `datasetItem3` INT,
    PRIMARY KEY (`datasetID`, `datasetWeekOf`)
    )Engine = InnoDB;

CREATE TABLE IF NOT EXISTS `datasetCalculation`(
    `datasetID` INT NOT NULL,
    `datasetWeekOf` DATE,
    `datasetItemSum` INT,
    PRIMARY KEY (`datasetID`, `datasetWeekOf`),
    CONSTRAINT `datasetID_FK`
        FOREIGN KEY (`datasetID`)
        REFERENCES `dataset` (`datasetID`)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT `datasetWeekOf_FK`
        FOREIGN KEY (`datasetID`)
        REFERENCES `dataset` (`datasetID`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
    ) Engine = InnoDB;

我想做的是什么时候dataset用下周的信息更新(每周更新一次),自动将相关信息插入到datasetCalculation. 等效的方法是用某种语言(比方说 php)计算 的总和datasetItem(1-3),然后将其插入 datasetCalculation. datasetItemSum以及其他相关信息。相反,我希望这由数据库自动完成。

4

0 回答 0