Mysql - 如果语句,执行更新
我正在尝试构建一个可以从日期时间戳中添加/减去秒的单个查询。
这是我目前所拥有的
UPDATE `table`
SET end_dt = DATE_ADD(end_dt, INTERVAL (15 - TIMESTAMPDIFF(SECOND, NOW(), end_dt)) SECOND)
WHERE DATE_SUB(end_dt, INTERVAL 15 second) <= NOW()
基本上是将日期时间重置为距当前时间仅剩 15 秒(如果低于 15 秒)。
我想添加一个 IF 条件,如果根据“end_dt”剩余时间超过 15 秒,则只会添加一秒。
I'm trying to build a single query that can add/subtract seconds from a datetime stamp.
Here's what I have currently
UPDATE `table`
SET end_dt = DATE_ADD(end_dt, INTERVAL (15 - TIMESTAMPDIFF(SECOND, NOW(), end_dt)) SECOND)
WHERE DATE_SUB(end_dt, INTERVAL 15 second) <= NOW()
Basically it's to reset the datetime to only having 15 seconds left (if it's under 15 seconds) from the current time.
I'd like to add in an IF condition, to where if there are more than 15 seconds left based on the 'end_dt', it will only add one second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其拆分为两个更新查询,一个对应于预期 IF 语句的每个分支。
Split it into two update queries, one for each branch of your intended IF statement.
我不确定我是否理解这个问题,但这里有一个镜头:
这表示“如果 end_dt 是 15,则添加内容,否则保持不变。”我确信“end_dt = 15”不是您想要做的比较,但这应该会告诉您需要做什么。
I'm not sure I understand the question, but here's a shot:
This says, "if end_dt is 15, add stuff, otherwise leave it the same." I'm sure "end_dt = 15" isn't the comparison you want to do, but this should probably show you what you need to do.
使用 MySQL 的 IF 函数来完成此操作。
我将 end_dt 隔离在 WHERE 子句的一半上,因此可以使用 end_dt 上的索引(如果存在)。
您的意思是更新所有行吗?
Do it using MySQL's IF function.
I isolated the end_dt on one half of the WHERE clause so an index on end_dt could be used it if exists.
Do you mean to update ALL rows?