MYSQL:如何找到“上一个星期一的日期”(演出问题) [英] MYSQL: How can I find 'last monday's date' (performance Issue)
本文介绍了MYSQL:如何找到“上一个星期一的日期”(演出问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选择 date_sub(curdate(),间隔WEEKDAY(curdate())day) as LastMonday
从双重
解决方案
如果您不使用古代的MySQL,可以将其包装在一个存储的函数中。
CREATE FUNCTION`LastMonday`()RETURNS DATETIME
返回DATE_SUB(CURDATE(),INTERVAL WEEKDAY(CURDATE ) 天) ;
然后调用
选择LastMonday()作为LastMonday
更新:
如果您遇到性能问题,可以将该值保存在会话变量中。这样就可以确定它只能计算一次。
set @ LastMonday = LastMonday();
选择@Lastmonday;
(在这个简单的查询中,它没有任何区别...)
is there a simpler way than writing:
select date_sub(curdate(), interval WEEKDAY(curdate()) day) as LastMonday
from dual
解决方案
If you're not using an ancient MySQL, you can wrap this in a stored function.
CREATE FUNCTION `LastMonday`() RETURNS DATETIME
RETURN DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) ;
and then call
select LastMonday() as LastMonday
Update:
If you're having performance problems, you can persist the value in a session variable. That way you can be sure that it will only be calculated once.
set @LastMonday=LastMonday();
select @Lastmonday;
(in this simple query it makes no difference of course...)
这篇关于MYSQL:如何找到“上一个星期一的日期”(演出问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文