0

当月开始

<?php echo date("Y-m-1");?>

当前月底

<?php echo date("Y-m-31");?>


and this is mysql query

$result = mysql_query("SELECT * FROM fuel where monthdate BETWEEN 'current month start' AND 'current month end' ORDER BY CAST(amount as SIGNED INTEGER) DESC LIMIT 4");

如何回应这个

<?php echo date("Y-m-1");?>

   <?php echo date("Y-m-31");?>

在这个“{Ym-1}”和“{Ym-31}”之间

请帮我解决这个问题谢谢

4

4 回答 4

1

如果您需要查找当前年份和月份记录,您可以使用EXTRACTmysql 的功能,它可以为您提供当前月份并进行=比较

SELECT * FROM fuel where 
EXTRACT(YEAR_MONTH FROM `monthdate`) =
EXTRACT(YEAR_MONTH FROM CURRENT_DATE) 
ORDER BY CAST(amount as SIGNED INTEGER) 
DESC LIMIT 4

注意确保列monthdate数据类型正确,如date,datetime

提炼

于 2013-10-02T06:41:46.143 回答
1

您可以在纯 SQL 中轻松做到这一点,而无需在 PHP 中花费时间:

"SELECT * FROM fuel WHERE YEAR(monthdate) = YEAR(CURDATE()) AND MONTH(monthdate) = MONTH(CURDATE()) ORDER BY CAST(amount as SIGNED INTEGER) DESC LIMIT 4"

此外,您不能指望无效日期会像这样工作。

在 SQL 查询中试试这个:

`SELECT DATE('2013-02-28'), DATE('2013-02-31')`

它将返回2013-02-28NULL。MySQL 需要一个 VALID 日期,否则它将不起作用。

于 2013-10-02T06:39:17.860 回答
0

如果您需要选择给定月份的所有内容,请使用 date_format:

$myDate = date('Y-m');
$sql = "SELECT * FROM `fuel` WHERE DATE_FORMAT('%Y-%m', `monthdate`) = '$myDate'";
于 2013-10-02T06:38:18.790 回答
0

试试这个代码很好。

$result = mysql_query(
    "SELECT * FROM fuel where monthdate BETWEEN (SELECT DATE_FORMAT( NOW( ) , '%Y-%m-1' )) AND (SELECT DATE_FORMAT( NOW( ) , '%Y-%m-30' )) ORDER BY CAST(amount as SIGNED INTEGER) DESC LIMIT 4"
);
于 2014-06-24T12:43:14.303 回答