如何从 mysql 中的日期获取月份

发布于 2024-08-18 03:33:55 字数 442 浏览 6 评论 0原文

我希望能够使用这样的语句从 mysql 获取结果:

SELECT * 
  FROM table 
 WHERE amount > 1000 

但是我想获取限制在某个月份和年份的结果(基于用户的输入)...我正在尝试这样:

SELECT * 
  FROM table 
 WHERE amount > 1000 
   AND dateStart = MONTH('$m')   

... $m 是一个月,但它给出了错误。

在该表中,它实际上有两个日期:startDateendDate,但我重点关注 startDate。输入值可以是月份和年份。如何表达根据当年该月获取结果的 SQL 语句?

I want to be able to fetch results from mysql with a statement like this:

SELECT * 
  FROM table 
 WHERE amount > 1000 

But I want to fetch the result constrained to a certain a month and year (based on input from user)... I was trying like this:

SELECT * 
  FROM table 
 WHERE amount > 1000 
   AND dateStart = MONTH('$m')   

...$m being a month but it gave error.

In that table, it actually have two dates: startDate and endDate but I am focusing on startDate. The input values would be month and year. How do I phrase the SQL statement that gets the results based on that month of that year?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

茶色山野 2024-08-25 03:33:55

您很接近 - 向后进行比较(假设 startDate 是 DATETIME 或 TIMESTAMP 数据类型):

SELECT * 
  FROM table 
 WHERE amount > 1000 
   AND MONTH(dateStart) = {$m}

注意事项:


替代方案:


因为对列使用函数不能使用索引,所以更好的方法是使用 BETWEENSTR_TO_DATE 函数:

WHERE startdate BETWEEN STR_TO_DATE([start_date], [format]) 
                    AND STR_TO_DATE([end_date], [format])

请参阅格式化语法文档。

参考:


You were close - got the comparison backwards (assuming startDate is a DATETIME or TIMESTAMP data type):

SELECT * 
  FROM table 
 WHERE amount > 1000 
   AND MONTH(dateStart) = {$m}

Caveats:


Alternatives:


Because using functions on columns can't use indexes, a better approach would be to use BETWEEN and the STR_TO_DATE functions:

WHERE startdate BETWEEN STR_TO_DATE([start_date], [format]) 
                    AND STR_TO_DATE([end_date], [format])

See the documentation for formatting syntax.

Reference:


凝望流年 2024-08-25 03:33:55

使用month() 函数。

select month(now());

Use the month() function.

select month(now());
几味少女 2024-08-25 03:33:55

试试这个:

SELECT * 
FROM table 
WHERE amount > 1000 AND MONTH(dateStart) = MONTH('$m') AND YEAR(dateStart) = YEAR('$m')

Try this:

SELECT * 
FROM table 
WHERE amount > 1000 AND MONTH(dateStart) = MONTH('$m') AND YEAR(dateStart) = YEAR('$m')
毅然前行 2024-08-25 03:33:55

例如,

$date = sprintf("'%04d-%02d-01'", $year, $month);
$query = "
  SELECT
    x,y,dateStart
  FROM
    tablename
  WHERE
    AND amount > 1000
    AND dateStart >= $date
    AND dateStart < $date+Interval 1 month
";
mysql_query($query, ...

这将创建一个查询,例如

WHERE
  AND amount > 1000
  AND dateStart >= '2010-01-01'
  AND dateStart < '2010-01-01'+Interval 1 month

+ Interval 1 Monthdate_add()

SELECT Date('2010-01-01'+间隔1个月)-> 2010-02-01
SELECT Date('2010-12-01'+间隔1个月)-> 2011-01-01
这样您始终可以获得下个月的第一天。您想要的记录的 dateStart 必须早于该日期,但晚于/等于您传递给 sprintf() 的月份(和年份)的第一天。
'2010-01-01'+Interval 1 Month 在行之间不会更改。 MySQL 只会计算该术语一次,并且可以利用索引进行搜索。

E.g.

$date = sprintf("'%04d-%02d-01'", $year, $month);
$query = "
  SELECT
    x,y,dateStart
  FROM
    tablename
  WHERE
    AND amount > 1000
    AND dateStart >= $date
    AND dateStart < $date+Interval 1 month
";
mysql_query($query, ...

This will create a query like e.g.

WHERE
  AND amount > 1000
  AND dateStart >= '2010-01-01'
  AND dateStart < '2010-01-01'+Interval 1 month

+ Interval 1 month is an alternative to date_add().

SELECT Date('2010-01-01'+Interval 1 month)-> 2010-02-01
SELECT Date('2010-12-01'+Interval 1 month)-> 2011-01-01
This way you always get the first day of the following month. The records you want must have a dateStart before that date but after/equal to the first day of the month (and year) you've passed to sprintf().
'2010-01-01'+Interval 1 month doesn't change between rows. MySQL will calculate the term only once and can utilize indices for the search.

挽容 2024-08-25 03:33:55

试试这个

SELECT * 
      FROM table 
     WHERE amount > 1000 
       AND MONTH(datestart)
    GROUP BY EXTRACT(YEAR_MONTH FROM datestart)

Try this

SELECT * 
      FROM table 
     WHERE amount > 1000 
       AND MONTH(datestart)
    GROUP BY EXTRACT(YEAR_MONTH FROM datestart)
久而酒知 2024-08-25 03:33:55

如果(日期字段是文本,然后将此字符串转换为日期),请尝试以下操作:

SELECT * FROM `table_name` WHERE MONTH(STR_TO_DATE(date,'%d/%m/%Y'))='11'

//This will give month number MONTH(STR_TO_DATE(date,'%d/%m/%Y'))
//If its return 11 then its November
// Change date format with your date string format  %d/%m/%Y

Try this if(date field is text then convert this string to date):

SELECT * FROM `table_name` WHERE MONTH(STR_TO_DATE(date,'%d/%m/%Y'))='11'

//This will give month number MONTH(STR_TO_DATE(date,'%d/%m/%Y'))
//If its return 11 then its November
// Change date format with your date string format  %d/%m/%Y
似梦非梦 2024-08-25 03:33:55

适用于:MySQL 5.7、MySQL 5.6、MySQL 5.5、MySQL 5.1、MySQL 5.0、MySQL 4.1、MySQL 4.0、MySQL 3.23

  • 日:

    SELECT EXTRACT(DAY FROM "2017-06-15");
    
  • 月:

    SELECT EXTRACT(MONTH FROM "2017-06-15");
    
  • 年份:

    SELECT EXTRACT(YEAR FROM "2017-06-15");
    年份

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

  • Day:

    SELECT EXTRACT(DAY FROM "2017-06-15");
    
  • Month:

    SELECT EXTRACT(MONTH FROM "2017-06-15");
    
  • Year:

    SELECT EXTRACT(YEAR FROM "2017-06-15");
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文