我想用Yii Framework从数据库中选择数据。我的查询在下面
$sql = Yii::app()->db->createCommand()
->select('a.status, b.date')
->from('check as b')
->join('patient as a', 'a.noRM = b.noRM')
->join('test_result as c', 'b.ID = c.ID')
->where('b.date between '.$start_date.' and '.$end_date)
->queryAll();
我认为它有一些问题,因为它给我零(array()={}
),而在数据库中有数据,其中$start_date
和$end_date
之间的日期字段。
如果我更改语句而没有子句where,那么我的语法变为:
$sql = Yii::app()->db->createCommand()
->select('a.status, b.date')
->from('check as b')
->join('patient as a', 'a.noRM = b.noRM')
->join('test_result as c', 'b.ID = c.ID')
->queryAll();
结果是真的,没什么问题。或者我用:
更改我的语法$sql = Yii::app()->db->createCommand()
->select('a.status, b.date')
->from('check as b')
->join('patient as a', 'a.noRM = b.noRM')
->join('test_result as c', 'b.ID = c.ID')
->where('b.noRM = '.$noRM)
->queryAll();
结果也是如此。所以我认为选择数据时存在一些问题取决于日期。
答案 0 :(得分:1)
初始sql查询中的问题是server{
listen 8082;
server_name yii2.dev;
access_log logs/yii2.access.log;
error_log logs/yii2.error.log error;
root /home/admin/web/nginx/html/basic/web/;
location / {
index index.html index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
}
'
->where()
您应该将每个日期都包含在->where('b.date between '.$start_date.' and '.$end_date) // Wrong
这样的
'date'
所以在你的最终sql查询中,它看起来像->where("b.date between '".$start_date."' and '".$end_date."'") // Correct
答案 1 :(得分:1)
Try This:-
$criteria = new CDbCriteria;
$criteria->addBetweenCondition('attributeName', 'value1', 'value2');
$user = Users::model()->findAll($criteria);