如何将count(*)和按查询分组转换为yii并从中获取数据 [英] how to convert count(*) and group by queries to yii and fetch data from it

查看:1537
本文介绍了如何将count(*)和按查询分组转换为yii并从中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在yii中转换此查询

I want to convert this query in yii

SELECT count(*) AS cnt, date(dt) FROM tbl_log where status=2 GROUP BY date(dt) 

并从中获取数据。我尝试这个命令( dt 是datetime字段):

and fetch data from that. I try this command (dt is datetime field):

$criteria = new CDbCriteria();
$criteria->select = 'count(*) as cnt, date(dt)';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);

但不会获取任何数据!
wath我可以取得数据吗?

but no data will fetch! wath can I do to get data?

推荐答案

可能你没有看到数据,不存在。

Probably you see no data because you need assign data to model attributes which doesn't exist.

$criteria = new CDbCriteria();
$criteria->select = 'count(*) AS cnt, date(dt) AS dateVar';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);

这意味着您的模型必须具有属性cnt和dateVar才能显示您的数据。如果你需要自定义查询,请检查Hearaman的回答。

This means that your model must have attributes cnt and dateVar in order to show your data. If you need custom query then check Hearaman's answer.

这篇关于如何将count(*)和按查询分组转换为yii并从中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆