[英]Count records this month
I have a table that stores dates in the format of MM/DD/YY (field name is date). 我有一个表,以MM / DD / YY格式存储日期(字段名称是日期)。 I need to have a count that totals just the records for the current month. 我需要统计一下本月的记录总数。 From searching, this is what I currently have but something is not right. 通过搜索,这是我目前拥有的东西,但是不正确。 Do I need to do a convertToDate, is it that my field is called "date", or am I missing something entirely? 我需要做一个convertToDate吗,是我的字段被称为“日期”,还是我完全丢失了什么?
$totalcount = mysql_query("select count(*) as 'total'
FROM state_to_state
WHERE status = 99 AND type = 1
AND MONTH(`date`) = MONTH(CURDATE())
AND YEAR(`date`) = YEAR(CURDATE())");
$totalnum = mysql_fetch_array($totalcount);
if($totalnum['total'] > 0) { $month_status = $totalnum['total']." this Month. "; }
Your dates are NOT native mysql dates (varchar, probably?), so you cannot use the mysql date functions on them directly. 您的日期不是本机mysql日期(可能是varchar),因此您不能直接在其上使用mysql日期功能。 date('MM/DD/YY')
will not work reliably: date('MM/DD/YY')
将无法可靠运行:
mysql> select month('03/03/14'), month('03/18/14');
+-------------------+-------------------+
| month('03/03/14') | month('03/18/14') |
+-------------------+-------------------+
| 3 | NULL |
+-------------------+-------------------+
Convert your date fields to native mysql date types, or take a huge hit on performance and convert them to native date values onthefly via str_to_date() . 将日期字段转换为本地mysql日期类型,或者对性能产生重大影响,然后通过str_to_date()将它们即时转换为本地日期值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.