开发者

MYSQL REPORTING

开发者 https://www.devze.com 2023-04-06 08:03 出处:网络
is there a more efficent way to get rows fro ma table from the current week, last week, current month and current year to this

is there a more efficent way to get rows fro ma table from the current week, last week, current month and current year to this

    // current month
    SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND MONTH( date_completed ) = MONTH( CURDATE( ) ) 

    // current week
    SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND WEEK( date_completed ) = WEEK( CURDATE( ) ) 

    // last week
    SELECT * 开发者_高级运维FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND WEEK( date_completed ) = WEEK( CURDATE( ) ) - 1 


// current year
SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) )


You should be able to use a query similar to this

SELECT * FROM crm_tasks 
WHERE date_completed >= @FirstDayOfYear AND date_completed < @FirstDayOfNextYear

Where @FirstDayOfYear and @FirstDayOfNextYear are filled in appropriately. If you have an index on date_created, this should work reasonably fast. This can be altered for any period you want to use.

0

精彩评论

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