mysql 转换为间隔
MySQL 中是否可以将其转换为间隔?
我想将查询中的值评估为间隔,如下所示:
select DATE_SUB(NOW(),CAST(user.expiry_interval AS interval))
其中 user.expiry_interval 是 'INTERVAL 1 WEEK' 或类似的内容
select DATE_SUB(NOW(),CAST('INTERVAL 1 week' AS INTERVAL))
is it possible to cast as an interval in MySQL?
I want to evaluate a value from the query as an interval, like this:
select DATE_SUB(NOW(),CAST(user.expiry_interval AS interval))
where user.expiry_interval is 'INTERVAL 1 WEEK' or something like that
select DATE_SUB(NOW(),CAST('INTERVAL 1 week' AS INTERVAL))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
INTERVAL
不是数据类型,但是关键字可以将人类-可读的“持续时间”字符串到整数。因此,“转换为INTERVAL
”是没有意义的。如果您的“间隔”输入是可变的,那么这可以工作,但您不能从字符串中获取整个内容,就像您不能将字符串“SELECT * FROM tbl”“转换”为查询表达式。您可以使用变量作为数字操作数(见下文),但我认为就是这样。
如果您想要比这更强大的逻辑,则必须求助于 CASE。或者...首先存储整数持续时间。
INTERVAL
is not a data type, but a keyword that converts a human-readable "duration" string into an integer. As such, "casting toINTERVAL
" makes no sense.If your "interval" input is variable, then this can work but you can't take the entire thing from a string any more than you can "cast" the string
"SELECT * FROM tbl"
into a query expression. You can use a variable for the numeric operand (see below) but I think that's it.You'll have to resort to
CASE
if you want logic that's stronger than that. Or... store integer durations in the first place.