简体   繁体   English

仅当更新另一个表上的列或向另一个表添加新记录时,才将记录插入到新的MySQL数据库表中吗?

[英]Insert record into new MySQL Database table only when a Column on another Table is Updated or a New record is added to that other Table?

I have the MySQL SQL below that is ran on my Project Management application I am building every time Project Tasks are Saved. 每次保存项目任务时,我都会在正在构建的项目管理应用程序中运行以下MySQL SQL。

A save can consist of saving new tasks records as well as updating existing records. 保存可以包括保存新任务记录以及更新现有记录。 For this reason I have this fancy SQL below which Inserts a New Tasks record if one doesn't exist yet with that ID, if it does already exist then it updates certain fields. 出于这个原因,我在下面有一个花哨的SQL,如果尚不存在具有该ID的记录,则在其下面插入一个New Tasks记录;如果该记录已经存在,则它将更新某些字段。 It is smart enough that it does NOT update the date_modified column unless name , description , status , type , priority fields have changed from their previous value. 足够聪明,它不会更新date_modified列,除非namedescriptionstatustypepriority字段已从其先前值更改。 The reason for this is because the sort_order column will change on every record update but should not constitute a " change " in the date_modified field if only the sort_order column is updated. 这样做的原因是因为sort_order列将在每次记录更新时更改,但如果只更新sort_order列,则date_modified字段中不应构成“ change ”。

The reason it works this way is because there is 1 form on a page that loads all the Tasks records for a project, so you can mass edit all the records at one time as well as add new Tasks records at the bottom. 之所以这样工作,是因为页面上有1个表单可以加载项目的所有Tasks记录,因此您可以一次批量编辑所有记录,也可以在底部添加新的Tasks记录。 So this SQL below gets called in a WHILE loop for every Tasks record being saved. 因此,下面的此SQL在WHILE循环中为每个要保存的Tasks记录调用。

It works amazing so far, just as I had hoped but now I am at another challenge. 到目前为止,它的运行效果惊人,正如我所希望的那样,但是现在我面临着另一个挑战。

I would like to create a new Database table for Updates which will basically insert a new record every time a NEW Tasks record is added and every time a Task record is UPDATED. 我想为更新创建一个新的数据库表,该表基本上将在每次添加新任务记录和每次任务记录更新时都插入一个新记录。

An example would be when I update a page with 20 Project Tasks and I change the values on 5 of the Tasks records, it would then insert 5 new records into my updates table. 例如,当我用20个项目任务更新页面并更改5个任务记录上的值时,它将在我的更新表中插入5个新记录。 Something like... 就像是...

JasonDavis Updated Task Record #1
JasonDavis Updated Task Record #2
JasonDavis Updated Task Record #3
JasonDavis Updated Task Record #4
JasonDavis Updated Task Record #5
JasonDavis ADDED NEW Task Record #4534

With the way my SQL is ran below, I am not able to know in my PHP which records were actually Updated as it updated every single record, I need to insert into my new table, only records where the date_modified is Updated. 用我的SQL运行方式,我无法在PHP中知道实际上更新了哪些记录,因为它更新了每条记录,因此我需要插入到新表中,仅更新date_modified记录。


QUESTION 1): Does anyone have any ideas how I might achieve this goal? 问题1):是否有人对我如何实现这一目标有任何想法? Modifying my SQL below or another method to make sure I insert a new record into an UPDATES DB Table when a New Tasks record is created, OR when a Task record has ANY of these columns Updated: name , description , status , type , priority , or date_modified is UPDATED . 修改下面的SQL或其他方法,以确保在创建新任务记录时或在任务记录具有以下任何列更新时,将新记录插入到UPDATES数据库表中: namedescriptionstatustypepriority ,或date_modified为UPDATED This means if a Task record updates only a field like sort_order then it will NOT insert a new record into the UPDATES table for that updated Task record. 这意味着,如果任务记录仅更新诸如sort_order类的字段,则它将不会在该更新的任务记录的UPDATES表中插入新记录。

I have seen some posts on StackOverflow that do somewhat similar but still far less complex items using TRIGGERS which I have never used a Trigger or even heard of them until recently so I have no idea if that is how I would HAVE to do it and if it is, how I would make it do such a complex conditional insert into my new UPDATES table. 我在StackOverflow上看到过一些类似的文章,但使用TRIGGERS却不那么复杂,直到最近我才从未使用过Trigger甚至从未听说过这些文章,所以我不知道那是我应该怎么做以及是否就是说,我将如何在新的UPDATES表中进行如此复杂的条件插入。

My ultimate goal is to simply create a new UPDATES table which will act as a stream showing which tasks are created and modified and by which user and the DATETIME the event happened. 我的最终目标是简单地创建一个新的UPDATES表,该表将作为流显示创建和修改的任务以及事件发生的用户和日期时间。 What complicates it is my existing INSERT or UPDATE SQL below has to continue working how it does now but somehow get the correct Tasks that are UPDATED accordingly to the conditions mentioned above to constitute a new insert record into the new UPDATES table. 使问题变得复杂的是,我下面的现有INSERT或UPDATE SQL现在必须继续工作,但如何才能获得正确的任务,这些任务必须根据上述条件进行相应更新,才能在新的UPDATES表中构成新的插入记录。 Please help experts!? 请高手帮忙!?


$sql = "
    INSERT INTO
        project_tasks(task_id, project_id, created_by_user_id, modified_user_id, name, description, status, priority, type, date_entered, date_modified, date_started, date_completed, sort_order, heading)
    VALUES
        ('$taskId', '$projectId', '$created_by_user_id', '$modified_user_id', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$date_started', '$date_completed', '$sort_order', '$heading')
    ON DUPLICATE KEY UPDATE
        date_modified = (CASE
            WHEN name <> values(name)
            OR description <> values(description)
            OR status <> values(status)
            OR type <> values(type)
            OR priority <> values(priority)
              THEN UTC_TIMESTAMP()
              ELSE date_modified
        END),
        modified_user_id='$modified_user_id',
        name='$name',
        description='$description',
        status='$status',
        priority='$priority',
        type='$type',
        date_started='$date_started',
        date_completed='$date_completed',
        sort_order='$sort_order',
        heading='$heading'";

 return $this->db->query($sql);

I started experimenting with Triggers and they seem to be my ticket, they are working and doing my end goal! 我开始试验扳机 ,它们似乎是我的入场券,它们正在工作并正在实现我的最终目标!

For Task INSERTS I have this trigger... 对于任务插入,我有此触发器...

DROP TRIGGER IF EXISTS project_task_new_stream;
CREATE TRIGGER `project_task_new_stream` AFTER INSERT ON `apoll_web_projects_tasks`
FOR EACH ROW
    INSERT INTO apoll_web_projects_updates_stream (event_type, record_id, modified_user_id, record_name, description, date_entered) VALUES ('0', NEW.task_id, NEW.modified_user_id, NEW.name, NEW.description, NEW.date_modified);

For Task UPDATES I have this trigger ...which only creates an update stream record IF the date_modified field is changed! 对于Task UPDATES,我有这个触发器 ...如果date_modified字段被更改,它只会创建一个更新流记录!

DROP TRIGGER IF EXISTS project_task_updates_stream;
DELIMITER $$
CREATE TRIGGER `project_task_updates_stream` AFTER UPDATE ON `apoll_web_projects_tasks` FOR EACH ROW
BEGIN
IF NOT (NEW.date_modified <=> OLD.date_modified)
    THEN
        INSERT INTO apoll_web_projects_updates_stream (event_type, record_id, modified_user_id, record_name, description, date_entered) VALUES ('0', NEW.task_id, NEW.modified_user_id, NEW.name, NEW.description, NEW.date_modified);
    END IF;
END $$
DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM