将字段设置为在UPDATE时自动插入时间戳?

编程入门 行业动态 更新时间:2024-10-11 19:23:05
本文介绍了将字段设置为在UPDATE时自动插入时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个表,其中有一个名为product_price的 DEC 字段,我想添加一个名为price_updated_date的字段.有没有一种方法可以将表设置为在product_price字段更新时自动插入当前时间戳?

I have a table with a DEC field named product_price and I wanted to add a field called price_updated_date. Is there a way to set the table to automatically insert the current time-stamp whenever the product_price field has been updated?

如果没有,是否有办法将其设置为在条目完全更新时插入当前时间戳?

If not, is there a way to set it to insert the current time-stamp any time the entry is updated at all?

在这里使用触发器似乎是最好的选择.我是触发器的新手,在创建它时遇到了一些麻烦.这是我的代码:

It seems like using a trigger is the best option here. I am new to triggers and having some trouble creating it. Here is my code:

CREATE TRIGGER price_update AFTER UPDATE ON cart_product FOR EACH ROW IF(OLD.product_price != NEW.product_price) THEN UPDATE cart_product SET price_updated_date = CURDATE() WHERE product_id = NEW.product_id

这给了我这个错误:

#1064-您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以找到在第8行

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8

推荐答案

是的,在对表进行更新后创建触发器

yup, create trigger after update on your table

CREATE TRIGGER price_update AFTER UPDATE on _table_ FOR EACH ROW UPDATE _table_ SET price_updated_date = CURTIME() where id=NEW.id

更多推荐

将字段设置为在UPDATE时自动插入时间戳?

本文发布于:2023-10-26 17:49:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   设置为   时间   UPDATE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!