MySQL 仅在当月内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4654961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL only within the current month?
提问by newbtophp
I have the following MySQL query, and I'm trying to adjust it so it only fetches results which are within the current month (of the current year), I'm guessing you may require more info about my MySQL structure so here it goes - I have a UNIX timestamp generated by PHP's time()
stored within the time
column (under the referrals
table), so with the below setup it would be t2.time
.
我有以下 MySQL 查询,我正在尝试调整它,以便它只获取当前月份(当前年份)内的结果,我猜您可能需要有关我的 MySQL 结构的更多信息,所以在这里- 我有一个由 PHP 生成的 UNIX 时间戳time()
存储在time
列中(referrals
表下),因此使用以下设置,它将是t2.time
.
So my problem is I'm unsure how to proceed, I'm guessing it would be something like adding the following to end of the WHERE
clause? => AND t2.time IS WITHIN THE CURRENT MONTH
(caps are just on to distinguish problem from rest of query)but i'm not sure how to check if its within the current month.
所以我的问题是我不确定如何继续,我猜这类似于在WHERE
条款末尾添加以下内容?=> AND t2.time IS WITHIN THE CURRENT MONTH
(大写只是为了区分问题与查询的其余部分)但我不确定如何检查它是否在当月内。
The MySQL Query:
MySQL查询:
SELECT t1.username,
t1.website,
SUM(IF(t2.type = 'in', 1, 0)) AS in_count,
SUM(IF(t2.type = 'out', 1, 0)) AS out_count
FROM users AS t1
JOIN referrals AS t2
ON t1.username = t2.author
WHERE t1.website != ''
GROUP BY t1.username,
t1.website
ORDER BY in_count DESC
LIMIT 0, 10
Appreciate all help! :B
感谢所有帮助!:乙
回答by Anax
回答by ajreal
you can use from_unixtime
like
你可以使用from_unixtime
像
date_format(from_unixtime(t2.`time`), '%Y-%m')=date_format(now(), '%Y-%m')
But I think data type integer
is not so suitable for this requirement
但我认为数据类型integer
不太适合这个要求
I would think using datetime
will be more suitable, built an index on this column and this also make the filtering easier, like
我认为使用datetime
会更合适,在此列上建立索引,这也使过滤更容易,例如
t2.`time`>='2011-01-01' and t2.`time`<'2011-02-01'
or
或者
date_format(t2.`time`, '%Y-%m')=date_format(now(), '%Y-%m')
回答by Ryan Gooler
This is probably a lot more straightforward than you are going to expect.
这可能比您预期的要简单得多。
SELECT t1.username,
t1.website,
SUM(IF(t2.type = 'in', 1, 0)) AS in_count,
SUM(IF(t2.type = 'out', 1, 0)) AS out_count
FROM users AS t1
JOIN referrals AS t2
ON t1.username = t2.author
WHERE t1.website != ''
AND t2.time >= DATE_SUB( CURRENT_DATE, INTERVAL 1 DAY )
GROUP BY t1.username,
t1.website
ORDER BY in_count DESC
LIMIT 0, 10
回答by goat
where t2.time >= extract(YEAR_MONTH from CURRENT_DATE)
and t2.time < extract(YEAR_MONTH from CURRENT_DATE + INTERVAL 1 MONTH)
This assume t2.time is a datetime type. If its an integer unix timestamp, you can convert the upper and lower datetime boundaries we created, by using the UNIX_TIMESTAMP() function.
这假设 t2.time 是日期时间类型。如果它是一个整数 unix 时间戳,您可以使用 UNIX_TIMESTAMP() 函数转换我们创建的日期时间上限和下限。
回答by Cedric Simon
For better performances (--> use index), create an index on your date column and user a "between" in your where clause.
为了获得更好的性能(--> 使用索引),请在您的日期列上创建一个索引,并在您的 where 子句中使用“介于”。
select ... from my_table where my:date_field between concat(YEAR(CURRENT_DATE()),'-',MONTH(CURRENT_DATE()),'-01')
and adddate(concat(YEAR(CURRENT_DATE()),'-',MONTH(CURRENT_DATE()),'-01'), interval 1 month);