Skip to content

Conversation

@manandhar
Copy link

Currently the updated at column gets updated regardless of any changes even if there are none.

This will make sure the updated at get updated only if there are changes.

The sql is updated from

UPDATE `users` SET
  `name` = (CASE
                   WHEN `id` = '1' THEN 'John'
                   WHEN `id` = '2' THEN 'Jane'
                    ELSE `name` END),
`email` = (CASE
                 WHEN `id` = '1' THEN 'john@example.com'
                  WHEN `id` = '2' THEN 'jane@example.com'
                  ELSE `email` END),
`updated_at` = (CASE
                           WHEN `id` = '1' THEN '2024-10-30 12:00:00'
                            WHEN `id` = '2' THEN '2024-10-30 12:00:00'
                             ELSE `updated_at` END)
WHERE `id` IN("1","2");

to

UPDATE `users` SET
`name` = (CASE
        WHEN `id` = '1' THEN 'John'
        WHEN `id` = '2' THEN 'Jane'
        ELSE `name` END),
`email` = (CASE
        WHEN `id` = '1' THEN 'john@example.com'
        WHEN `id` = '2' THEN 'jane@example.com'
        ELSE `email` END),
`updated_at` = (CASE
        WHEN `id` = '1' AND (`name` != 'John' OR `email` != 'john@example.com') THEN '2024-10-30 12:00:00'
        WHEN `id` = '2' AND (`name` != 'Jane' OR `email` != 'jane@example.com') THEN '2024-10-30 12:00:00'
        ELSE `updated_at` END)
WHERE `id` IN("1","2");

@manandhar manandhar marked this pull request as draft November 2, 2025 22:26
@manandhar manandhar marked this pull request as ready for review November 6, 2025 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant