我正在尝试编写一个查询,该查询仅返回日期时间为 当月的第01天和当天的行。但我不确定是否可以做到。所以,我知道函数NOW()
给了我当前的DATETIME ,但我仍然不知道如何自动获取BETWEEN
的另一端应用程序,我不想让用户输入日期时间。
Ex:我想要您咨询当月的所有订单。
Select * from orders where date between (the function I am looking for) and NOW();
答案 0 :(得分:0)
select * from table_name
where (date between DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), interval 30 day), interval 1 day) AND CURDATE() )
或 你的方法..
select * from table_name
where (date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
或者您也可以这样做
select * from table_name
where `date` between curdate() - dayofmonth(curdate()) + 1
and curdate()
答案 1 :(得分:0)
这更好地使用索引(如果有的话)
DECLARE @iniDate DATE
DECLARE @endDate DATE
SET @endDate = curdate()
SET @iniDate = @endDate - dayofmonth(@endDate) + 1
SELECT *
FROM My_table
WHERE `My_date` BETWEEN @iniDate AND @endDate