简体   繁体   中英

MySQL BETWEEN clause with Yii PHP framework

Using Yii PHP framework, I produce the following query

SELECT * FROM `purchases` WHERE (date BETWEEN '2013-12-31' AND '2015-01-01')

with this code

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", ((int)$exportYear - 1) . "-12-31", ((int)$exportYear + 1) . "-01-01");
$purchases = Purchase::model()->findAll($criteria);

What I actually need is all 'purchases' that happened in year 2014. None from a minute before or after. What does the addBetweenCondition line need to be changed to in order to accomplish this?

The BETWEEN operator is inclusive.

If column type is DATE use '2014-01-01' AND '2014-12-31'

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", $exportYear . "-01-01", $exportYear . "-12-31");
$purchases = Purchase::model()->findAll($criteria);

If column type is DATETIME use '2014-01-01 00:00:00' AND '2014-12-31 23:59:59'

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", $exportYear . "-01-01 00:00:00", $exportYear . "-12-31 23:59:59");
$purchases = Purchase::model()->findAll($criteria);
SELECT * FROM `purchases` 
WHERE date BETWEEN '2014-01-01' AND '2014-12-31'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM