如何在YII 1.1上将此sql查询转换为CDB条件格式

yi0zb3m4  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(97)

我不知道如何将这个sql查询转换成yii CDB标准。请给予我一个想法来做。

$sql="SELECT ICode,name,Date ,COUNT(*) AS tot, COUNT(case progress when '1' then 1 else null end) AS complete 
      FROM  invoice i LEFT JOIN `customer` c  ON `i`.`CID` = `c`.`CID`  
      left outer join (SELECT * FROM invoiceitem ) ii on ii.IID = i.IID  
      GROUP by ICode , name , `Date` 
      order by complete DESC";

未定义属性“CDbCriteria.0”。

lymnna71

lymnna711#

你可以这样写:

<?php

 $db = Yii::app()->db; //THIS IS YOUR DATABASE CONNECTION
 //Query
 $result = $db->createCommand()->select([
        'ICode',
        'name',
        'Date',
        'COUNT(*) AS tot',
        'COUNT(CASE progress WHEN '1' THEN 1 ELSE null END) AS complete',
    ])->from('invoice AS i')
    ->leftJoin(
        'customer AS c',
        'i.CID = c.CID'
    )->leftJoin(
      '(SELECT * FROM invoiceitem ) AS ii',
      'ii.IID = i.IID'
    )->group('ICode, name, `Date`')->order('complete DESC')->queryAll();

相关问题