我正在尝试在Yii中使用FROM_UNIXTIME执行查询。以下代码无效:
$model = Banner::model()->findAll(array(
'condition' => 't.banner_kategori=:parent AND t.status=:status AND (FROM_UNIXTIME(t.publish_date) <= :datetime AND FROM_UNIXTIME(t.unpublish_date) >= :datetime OR t.publish_date = "" AND t.unpublish_date = "" OR FROM_UNIXTIME(t.publish_date) <= :datetime AND t.unpublish_date = "") ORDER BY created DESC LIMIT 10',
'params' => array(':parent' => $parent, ':status' => 1, ':datetime' => date('Y-m-d H:i:s')),
));
但是,当我运行此查询的SQL版本时,我得到了预期的结果:
SELECT *, FROM_UNIXTIME(publish_date) AS wew
FROM banner
WHERE banner_kategori=1
AND status=1 AND (FROM_UNIXTIME(publish_date) <= CURDATE() AND FROM_UNIXTIME(unpublish_date) >= CURDATE() OR publish_date = '' AND unpublish_date = '' OR FROM_UNIXTIME(publish_date) <= CURDATE() AND unpublish_date = '')
ORDER BY created DESC LIMIT 10
如何让第一个查询生效?
答案 0 :(得分:0)
你应该这样使用
$model = Banner::model()->findAll(array(
'condition' => 't.banner_kategori=:parent AND t.status=:status AND (FROM_UNIXTIME(t.publish_date) <= :datetime AND FROM_UNIXTIME(t.unpublish_date) >= :datetime OR t.publish_date = "" AND t.unpublish_date = "" OR FROM_UNIXTIME(t.publish_date) <= :datetime AND t.unpublish_date = "")',
'params' => array(':parent' => $parent, ':status' => 1, ':datetime' => date('Y-m-d H:i:s')),
'order'=>'created DESC',
'limit'=>10
));