HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

Update a date_modified MySQL Record Field ONLY when other fields have changed using CASE statement not working?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
casefieldupdatestatementdate_modifiedfieldschangedworkingmysqlrecord

Problem

I have asked my question on Stackoverflow but have not really gotten any answers so I am trying over here now.

I have the MySQL query that is ran in a PHP script below.

What it does is Inserts a new Project Task record if one does not exist with that ID number.

If a record exist with the ID number, then it UPDATE the fields that it should update.

The problem I have is I need it to only update the date_modified column when one of these columns value changes... name, description, status, type, or priority

public function addOrUpdateTaskRecord($taskId, $projectId, $name, $description, $status, $priority, $type, $date_entered, $date_modified, $sort_order, $heading){

    $sql = "
        INSERT INTO
            $this->tasksDbTableName(task_id, project_id, name, description, status, priority, type, date_entered, date_modified, sort_order, heading)
        VALUES
            ('$taskId', '$projectId', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$sort_order', '$heading')
        ON DUPLICATE KEY UPDATE
            name='$name',
            description='$description',
            status='$status',
            priority='$priority',
            type='$type',
            date_modified=UTC_TIMESTAMP(),
            sort_order='$sort_order',
            heading='$heading'";

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

}


Another user on here Gordon Linoff had showed me that I might be able to change this date_modified=UTC_TIMESTAMP(), and use this in place...

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)


This looked really promising and produces no errors in MySQL. It s

Solution

Below is my final working solution. It turns out that moving this...

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),


to the top of the list, above these fields...

name='$name',
description='$description',
status='$status',
priority='$priority',
type='$type',


So the final solution looks like this below...

$sql = "
    INSERT INTO
        $this->tasksDbTableName(task_id, project_id, name, description, status, priority, type, date_entered, date_modified, sort_order, heading)
    VALUES
        ('$taskId', '$projectId', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$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),
        name='$name',
        description='$description',
        status='$status',
        priority='$priority',
        type='$type',
        sort_order='$sort_order',
        heading='$heading'";


Just moving my CASE Statement to the top above the other set fields, made it start working correctly!

It almost seems as if these DB column fields name description status priority type were getting Updated before my Case statement could run which resulted in them always appearing to be the same as the DB fields and never any different so my Case statement was about useless. Moving it to the top, it now works 100% correctly.

Hopefully this will help someone with a similar problem someday as many people across 3 sites tried to fix it without success. Just moving it to the top did the trick though so the Order in this case very much matters!
]
Thanks to all that have contributed their time to try and find a solution, I always appreciate the help I receive on the StackExchange network sites!

Code Snippets

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),
name='$name',
description='$description',
status='$status',
priority='$priority',
type='$type',
$sql = "
    INSERT INTO
        $this->tasksDbTableName(task_id, project_id, name, description, status, priority, type, date_entered, date_modified, sort_order, heading)
    VALUES
        ('$taskId', '$projectId', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$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),
        name='$name',
        description='$description',
        status='$status',
        priority='$priority',
        type='$type',
        sort_order='$sort_order',
        heading='$heading'";

Context

StackExchange Database Administrators Q#77121, answer score: 3

Revisions (0)

No revisions yet.