我需要在当月的第一天和当天之间从MySQL数据库中选择数据。
select*from table_name
where date between "1st day of current month" and "current day"
有人可以提供此查询的工作示例吗?
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 date_sub(current_date(),interval dayofmonth(current_date())-1 day) as first_day_of_month;
我用过这个select DATE_ADD(DATE_SUB(LAST_DAY(now()), INTERVAL 1 MONTH),INTERVAL 1 day) first_day , LAST_DAY(now()) last_day, date(now()) today_day
我有一些类似的要求 - 对find first day of the month
但基于用户在他们的个人资料页面中选择的year end month
。
问题陈述 - 找到用户在其财政年度完成的所有txns
。 Financial year
是使用year end
月值确定的,其中month
可以是任何有效月份 - 1 for Jan
,2 for Feb
,3 for Mar
,.... 12 for Dec
。
对于一些客户financial year end
s在March
和一些观察它在December
。
Scenarios - (Today is `08 Aug, 2018`)
1. If `financial year` ends on `July` then query should return `01 Aug 2018`.
2. If `financial year` ends on `December` then query should return `01 January 2018`.
3. If `financial year` ends on `March` then query should return `01 April 2018`.
4. If `financial year` ends on `September` then query should return `01 October 2017`.
最后,下面是查询。 -
select @date := (case when ? >= month(now())
then date_format((subdate(subdate(now(), interval (12 - ? + month(now()) - 1) month), interval day(now()) - 2 day)) ,'%Y-%m-01')
else date_format((subdate(now(), interval month(now()) - ? - 1 month)), '%Y-%m-01') end)
其中?
是year end
月(值从1到12)。
SET @date:='2012-07-11';
SELECT date_add(date_add(LAST_DAY(@date),interval 1 DAY),
interval -1 MONTH) AS first_day
我正在寻找类似的查询,我需要在查询中使用一个月的第一天。 last_day
功能对我不起作用,但DAYOFMONTH
派上用场。
因此,如果有人正在寻找相同的问题,以下代码将返回当月第一天的日期。
SELECT DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY);
将日期列与该月的第一天进行比较:
select * from table_name where date between
DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY) and CURRENT_DATE
select * from table_name
where `date` between curdate() - dayofmonth(curdate()) + 1
and curdate()
我使用了以下查询。它过去对我很有用。
select date(now()) - interval day(now()) day + interval 1 day
试试这个 :
SET @StartDate = DATE_SUB(DATE(NOW()),INTERVAL (DAY(NOW())-1) DAY);
SET @EndDate = ADDDATE(CURDATE(),1);
select * from table where (date >= @StartDate and date < @EndDate);
mysql当前月份和当前年份的完整解决方案,它也正确地使用了索引:)
-- Current month
SELECT id, timestampfield
FROM table1
WHERE timestampfield >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY)
AND timestampfield <= LAST_DAY(CURRENT_DATE);
-- Current year
SELECT id, timestampfield
FROM table1
WHERE timestampfield >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFYEAR(CURRENT_DATE)-1 DAY)
AND timestampfield <= LAST_DAY(CURRENT_DATE);
select * from table
where date between
(date_add (CURRENT_DATE, INTERVAL(1 - DAYOFMonth(CURRENT_DATE)) day)) and current_date;
select * from <table>
where <dateValue> between last_day(curdate() - interval 1 month + interval 1 day)
and curdate();
一种不太正统的方法可能是
SELECT * FROM table_name
WHERE LEFT(table_name.date, 7) = LEFT(CURDATE(), 7)
AND table_name.date <= CURDATE();
作为一个月的第一个和现在之间的日期相当于本月的日期,现在之前。不过,我确实觉得这比其他方法更容易。