mysql return rows matching year month

后端 未结 3 870
独厮守ぢ
独厮守ぢ 2021-01-25 00:37

How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.

Timestamps for each row are for

相关标签:
3条回答
  • 2021-01-25 01:00
    select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12
    
    0 讨论(0)
  • 2021-01-25 01:06

    you could extract the year and month using a function, but that will not be able to use an index.

    if you want scalable performance, you need to do this:

    SELECT *
      FROM myTable
     WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
    
    0 讨论(0)
  • 2021-01-25 01:14

    I think EXTRACT is the function you are looking for:

    SELECT * FROM table
    WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
    
    0 讨论(0)
提交回复
热议问题