doushi7394 2017-09-19 15:05
浏览 146
已采纳

MySQL更新 - 如果日期尚未过去,则设置字段

I have a question regarding case/if statements in MySQL in regards to updating a field based on the possibility of a date in a field. I have two fields. One that contains a date ("LastDayToNotify") and one that contains a plaintext message ("NotifyMessage")

The date field can contain one of three values - An empty field, a Null, or a Date.

My question is - Is it possible, and if so how, to in MySQL to have a query that sets one field's value (NotifyMessage) based on if the date (LastDayToNotify) has passed? An example would be :

If the date has not passed (the date is today or before), update the NotifyMessage field to say "Generic Message"

but if the date field is empty, null, or it is passed it does nothing.

I've tested a couple modifactions of answers in this thread, but have been unable to successfully account for all 3 possible date scenarios. Here

I know this is probably a rookie question, but I've never encountered this particular need yet.

Thanks!

  • 写回答

3条回答 默认 最新

  • doulu3399 2017-09-19 15:10
    关注

    I'm assuming you're using sane field types (date or datetime)...

    UPDATE table
    SET
        NotifyMessage = 'Generic Message'
    WHERE
        LastDayToNotify IS NOT NULL
        AND LastDayToNotify >= CURDATE();
    

    If you're not already storing the dates as date types, take a look at: Converting a date in MySQL from string field

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器