5

怎么把这个sql语句转成yii格式建模型?

  SELECT  DISTINCT agency_id, university_id 
      FROM `tbl_universityagency` where agency_id=1

像这样我错过了什么?,

$criteria = new CDbCriteria();
        $criteria->distinct=true;            
        $criteria->condition = "agency_id=".$result->agency_id ;         
        $modal=Universityagency::model()->find($criteria); 
4

3 回答 3

6
$criteria = new CDbCriteria();
$criteria->distinct=true;
$criteria->condition = "agency_id=".$result->agency_id ;      
$criteria->select = 'id, agency_id, university_id';
$modal=Universityagency::model()->find($criteria);

应该产生查询:

SELECT  DISTINCT id,agency_id, university_id FROM `tbl_universityagency` where agency_id=1 LIMIT 1
于 2013-07-09T06:10:57.443 回答
4

您必须使用findAll()而不是find().

于 2013-07-09T06:17:32.167 回答
1

快速解决!

将以下代码放入您的模型文件中。

$criteria = new CDbCriteria;
$criteria->distinct = true;
$criteria->condition = "t.C = t.D";                
$criteria->group = "t.A";

如果您对此有任何疑问/疑虑,请告诉我们。

谢谢!

于 2015-06-30T11:00:16.890 回答