简体   繁体   English

使用查询构建器Yii选择特定期间之间的年和月的日期

[英]select date where year and month between certain period with Query builder Yii

I want to select data from database where date field is between certain period. 我想从日期字段介于特定时间段之间的数据库中选择数据。 For example: between January 2015 and April 2015. 例如:2015年1月至2015年4月。

$Q1 = Yii::app()->db->createCommand()
                ->select('count(kode_lelang) as totTender')
                ->from('proc_lelang')
                ->where('year(waktu_mulai)='.$year and 'month(waktu_mulai) between 1 and 4')
                ->queryAll();

But I do not think that I am writing the right syntax. 但是我不认为我在编写正确的语法。 so, Please help me. 所以,请帮助我。

I think the error is that the 'and' is not inside the string: 我认为错误是'and'不在字符串内:

$Q1 = Yii::app()->db->createCommand()
                ->select('count(kode_lelang) as totTender')
                ->from('proc_lelang')
                ->where('year(waktu_mulai)='{$year}' and month(waktu_mulai) between 1 and 4')
                ->queryAll();

There is a misplaced quote in your where statement. 您的where语句中的引号放错了位置 It's simpler to use double quotes and variable replacement instead. 而是使用双引号和变量替换来代替。

->where("year(waktu_mulai)={$year} and month(waktu_mulai) between 1 and 4")

Please not that this is dangerous if the user can control the content of variable $year as this could lead to SQL injections. 请不要担心,如果用户可以控制变量$year的内容,这将很危险,因为这可能导致SQL注入。 Check the Yii docs on how to prevent SQL injections. 查看Yii文档,了解如何防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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