doushifang4382 2018-05-15 16:58
浏览 46
已采纳

如果加入日期为六个月,则从表中删除记录

I want to delete a record from table if the field joindate is six months old or older.

Suppose An employee's joindate date is 15-06-2018 and on 15-11-2018 the user should be deleted

I have stored joindate and datetime. now trying to create a query that will delete a record if current date equals joindate + 6month.

  • 写回答

1条回答 默认 最新

  • dougouqin0763 2018-05-15 17:06
    关注

    Seems like a pretty simple expression.

    joindate < DATE(NOW()) + INTERVAL -6 MONTH
    

    take current date, subtract 6 months from it, and then compare to the joindate column, to find out if joindate is earlier.

    We can test that in a query:

    SELECT t.joindate < DATE(NOW()) + INTERVAL -6 MONTH `older_than_six_months`
         , t.joindate 
         , t.id
         , ... 
      FROM mytable t
     ORDER BY t.joindate ASC
    

    The expression will return 1 (true) if joindate is earlier than six months, or return 0 (false) or NULL.

    We can specify a specific date value in place of DATE(NOW()) for testing

    SELECT t.joindate < DATE(NOW()) + INTERVAL -6 MONTH `older_than_six_months`
         , t.joindate < '2018-07-01' + INTERVAL -6 MONTH `six_months_20180701`
         , t.joindate 
         , t.id
         , ... 
      FROM mytable t
     ORDER BY t.joindate ASC
    

    Once we have an expression that is tested, we can use it in a DELETE statement

    DELETE t.*
      FROM mytable t
     WHERE t.joindate < DATE(NOW()) + INTERVAL -6 MONTH 
    

    (Personally, I would do the less than or less than or equal comparison test < or <= rather than an equals test.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改