tmartin314 tmartin314 - 6 months ago 13
SQL Question

How go get entry from last year MySQL

This part of the query doesn't seem to be working

$iThisYear = date('Y');
$iLastYear = $iThisYear-1;

SELECT * FROM `{$this->_sPrefix}clicks`
WHERE `affiliate_id` = '{$iAid}'
AND `raw` = '1'
AND YEAR(`date`) = '{$iLastYear}'


It grabs from this year still. Does YEAR work?

Answer

Could you use DATE_SUB instead

SELECT * FROM `{$this->_sPrefix}clicks` 
WHERE `affiliate_id` = '{$iAid}' 
AND `raw` = '1' 
AND YEAR(`date`) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))
Comments