How to get first day of every corresponding month in mysql?

后端 未结 13 1843
野的像风
野的像风 2020-11-27 18:03

I want to get first day of every corresponding month of current year. For example, if user selects \'2010-06-15\', query demands to run from \'2010-06-01\' instead of \'2010

相关标签:
13条回答
  • 2020-11-27 19:10

    Slow (17s):

    SELECT BENCHMARK(100000000, current_date - INTERVAL (day(current_date) - 1) DAY); 
    SELECT BENCHMARK(100000000, cast(DATE_FORMAT(current_date, '%Y-%m-01') as date));
    

    If you don't need a date type this is faster: Fast (6s):

    SELECT BENCHMARK(100000000, DATE_FORMAT(CURDATE(), '%Y-%m-01'));
    SELECT BENCHMARK(100000000, DATE_FORMAT(current_date, '%Y-%m-01'));
    
    0 讨论(0)
提交回复
热议问题