简体   繁体   English

无法获取DISTINCT列值:

[英]Not able to get DISTINCT column values :

I am working in Yii and, I have a table named : visits 我在Yii工作,并且有一个名为:visits的表

It has two foreign keys : from_user_id and to_user_id which are linked to the 'user' table. 它有两个外键:from_user_id和to_user_id,它们链接到“用户”表。 Now the table visits has many same 'from_user_id' and I want to get retrieve them in Yii as DISCTINCT. 现在表访问有许多相同的“ from_user_id”,我想在Yii中以DISCTINCT检索它们。

This is my code : 这是我的代码:

$visited = Visit::model()->findAllByAttributes(array('to_user_id'=>Yii::app()->user->id));

$criteria = new CDbCriteria();
$criteria->distinct = true;

foreach($visited as $visits){

    echo User::model()->findByPk($visits->from_user_id,$criteria)->getFullName($visits->from_user_id);

   echo " <br>";
}

However, I am not able to get them as distinct. 但是,我无法使它们与众不同。

This is my output : 这是我的输出:

san
san
san
Leo
Leo

I want the output as : 我希望输出为:

San
Leo

This is not getting me distinct values. 这并没有给我带来独特的价值。 Where am I going wrong?? 我要去哪里错了?

I wasn't be able to test it, but apart from possible minor syntax adjustment, this should help you: 我无法对其进行测试,但是除了可能的较小语法调整之外,这应该可以帮助您:

$visited = Visit::model()->findAllByAttributes(
    array('to_user_id'=>Yii::app()->user->id),
    array('distinct' => True)
);

According to the doc, findAllByAttribute accept a condition or criteria parameter as second argument. 根据文档, findAllByAttribute接受条件或条件参数作为第二个参数。

If this is an array (according the doc for find() ), it will serve to initialize the various criteria properties of the requests. 如果这是一个数组(根据find()的文档),它将用于初始化请求的各种条件属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM