开发者

Using DAY(), WEEK(), and YEAR() at one query

开发者 https://www.devze.com 2023-01-15 00:27 出处:网络
i using MySQL Query for my task. And I interested using Date and time function. can i use DAY(), WEEK(), and YEAR() at one query?

i using MySQL Query for my task. And I interested using Date and time function. can i use DAY(), WEEK(), and YEAR() at one query?

SELECT Object 
  FROM table 
 WHERE DAY(date) BETWEEN 1 AND 7 
GROUP BY WEEK(date, 1), YEAR(date)

i want do this bcoz i'm worry if sometimes my program have an error because of the date setting and 开发者_运维问答not recognize some date.please give me an input.


Yes, you can use them all in a single query.

The only disadvantage I can think of is that using any of the DAY, WEEK or YEAR functions won't be able to use the index on the column the function is applied to, assuming one is present.

If you're having issues relating to date formatting, you should get familiar with:

  • DATE_FORMAT
  • STR_TO_DATE
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号