简体   繁体   English

SQL触发器-更新字段,其中日期早于当前日期

[英]SQL Trigger - Update field where date is older than current date

I've got a table of events, with a date on which they occur in the db. 我有一张事件表,其中有一个事件发生在数据库中的日期。 So naturally, when an event has finished, it needs to not be displayed on the website. 因此,自然而然地,当一个事件完成时,它不需要在网站上显示。 Currently I have a flag in the table for 'active'. 目前,我在表中有一个标记为“有效”。

I'm wondering what the best way to handle the events that have passed. 我想知道处理已发生事件的最佳方法是什么。 I thought the best way to be to create an event in phpmyadmin so that at 1am, all events that have gone past their date, will have their 'active' flags set to 0, but I heard that this is not the best way to do it, as it has more overhead and unnecessary i/o. 我认为最好的方法是在phpmyadmin中创建一个事件,以便在凌晨1点,所有超过其日期的事件都将其“活动”标志设置为0,但是我听说这不是最好的方法它,因为它有更多的开销和不必要的I / O。

Another option would be to handle it in the db query. 另一种选择是在数据库查询中处理它。 Select * from events where date > date AND active = 1. 从日期>日期AND有效= 1的事件中选择*。

Both work, but having an 'active' flagged event, that is not actually active sort of frustrates me. 两者都有效,但是有一个“活动”标记事件,实际上不是活动的,这让我感到沮丧。

What is the best way to handle this, and if a trigger/event in phpmyadmin is the way. 处理此问题的最佳方法是什么,如果是phpmyadmin中的触发器/事件,那是什么。 How do I go about that? 我该怎么办?

You can just write a view for this: 您可以为此编写一个视图:

create view v_table as
    select t.*, ( date > now() ) as IsActive;
    from table;

If you really want to have the burden of creating a trigger and maintaining the field, then you can do that. 如果您确实要承担创建触发器和维护字段的负担,那么可以这样做。 However, it doesn't seem worth it when a view handles this quite easily. 但是,当视图很容易处理时,似乎不值得。

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

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