我可以使用 yii CSqlDataProvider 执行两个或多个 sql 语句吗?

发布于 2024-12-29 03:20:14 字数 507 浏览 0 评论 0原文

我试图使用 yii 在以下 sql 语句中获得很少的结果,但是有一些问题......!

 $sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";

$count=count(Yii::app()->db->createCommand($sql)->query()); 

$dataProvider=new CSqlDataProvider($sql, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));

I am trying to get few result on following sql statements using yii, But there are something wrong....!!!

 $sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";

$count=count(Yii::app()->db->createCommand($sql)->query()); 

$dataProvider=new CSqlDataProvider($sql, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花想c 2025-01-05 03:20:14

您正在使用 count(Yii::app()->db->createCommand($sql)->query());,我不确定,但我认为 query() 仅返回 true 或 false。尝试使用 queryAll() 或者如果它只是单行,请使用 queryRow()

You are using count(Yii::app()->db->createCommand($sql)->query());, I am not sure, but i think query() just returns true or false. try using queryAll() or if it's just a single row use queryRow()

对不⑦ 2025-01-05 03:20:14
$sql="SET @pStat=-1; SET @pdt=0;  SELECT |selectPart| FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$selectPart = 'COUNT(1) as cnt';
$count = Yii::app()->db->createCommand(str_replace('|selectPart|', $selectPart, $sql))->queryScalar();
$sql = str_replace('|selectPart|','*',$sql);
$dataProvider=new CSqlDataProvider($sql, array(
        'totalItemCount'=>$count,
        'pagination'=>array(
            'pageSize'=> 10,
        ),
    ));
$sql="SET @pStat=-1; SET @pdt=0;  SELECT |selectPart| FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$selectPart = 'COUNT(1) as cnt';
$count = Yii::app()->db->createCommand(str_replace('|selectPart|', $selectPart, $sql))->queryScalar();
$sql = str_replace('|selectPart|','*',$sql);
$dataProvider=new CSqlDataProvider($sql, array(
        'totalItemCount'=>$count,
        'pagination'=>array(
            'pageSize'=> 10,
        ),
    ));
维持三分热 2025-01-05 03:20:14

尝试使用 CArrayDataProvider 而不是 CSqlDataProvider

$sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$result=Yii::app()->db->createCommand($sql)->queryAll(); 
$count=count($result);
$dataProvider=new CArrayDataProvider($result, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));

Try to use CArrayDataProvider instead of CSqlDataProvider:

$sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$result=Yii::app()->db->createCommand($sql)->queryAll(); 
$count=count($result);
$dataProvider=new CArrayDataProvider($result, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文