MySQL触发器问题:仅在列更改时触发?

发布于 2024-09-30 07:31:50 字数 195 浏览 0 评论 0原文

我不知道这是否可行,但我在表中有一个名为 active 的列。每当活动列发生更改时,我都想重置 date 列中的日期,但如果 active 列发生更改。

如果其他列发生更改,但 active 列未更改,则日期将保持不变。

I don't know if this is possible, but I have a column named active in a table. Whenever the active column gets changed, I would like to reset the date in the date column, but ONLY if the active column gets changed.

If other columns are changed but not the active column, then the date would remain the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-10-07 07:31:50

类似的东西

DELIMITER //
 CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable
     FOR EACH ROW
     BEGIN
     IF NEW.active <> OLD.active THEN
     SET NEW.date = '';     
     END IF;
     END
     //

something like

DELIMITER //
 CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable
     FOR EACH ROW
     BEGIN
     IF NEW.active <> OLD.active THEN
     SET NEW.date = '';     
     END IF;
     END
     //
冰雪之触 2024-10-07 07:31:50

在 #2 示例中,IF 测试遇到了问题。当其中一个值为空时,<>测试返回 null。这会导致测试未得到满足,并且即使该值根本不等于 null,触发器的操作也不会运行。为了解决这个问题,我想出了这个使用 <=> 的测试。 (NULL 安全等于)。希望这可以帮助别人。

DELIMITER $
DROP TRIGGER IF EXISTS updtrigger ;
$
CREATE TRIGGER updtrigger  AFTER UPDATE
    ON yourTable FOR EACH ROW
BEGIN
    IF ((NEW.active <=> OLD.active) = 0)  THEN
     SET NEW.date = '';     
     END IF;
$

Ran into an issue with the IF test in the #2 example. When one of the values is null the <> test returns null. This leads to the test not getting met and the action of the trigger will not get run even though the one value does not equal null at all. To fix this I came up with this test that uses <=> (NULL-safe equal). Hope this helps someone out.

DELIMITER $
DROP TRIGGER IF EXISTS updtrigger ;
$
CREATE TRIGGER updtrigger  AFTER UPDATE
    ON yourTable FOR EACH ROW
BEGIN
    IF ((NEW.active <=> OLD.active) = 0)  THEN
     SET NEW.date = '';     
     END IF;
$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文