如何在Yii中从一个有条件的表中选择两列的DISTINCT?

daupos2t  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(170)

如何将此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);
xiozqbni

xiozqbni1#

$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
h7appiyu

h7appiyu2#

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

flvlnr44

flvlnr443#

快解!
将下面的代码放入Model文件中。

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

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

相关问题