Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select DISTINCT of two columns from a table with condition in Yii?

Tags:

select

yii

How to convert this sql statement to yii format build a model?

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

like this what I am missing? ,

$criteria = new CDbCriteria();
        $criteria->distinct=true;            
        $criteria->condition = "agency_id=".$result->agency_id ;         
        $modal=Universityagency::model()->find($criteria); 
like image 986
raghul Avatar asked Jul 09 '13 05:07

raghul


People also ask

How do I select distinct two columns in MySQL?

To select distinct values in two columns, you can use least() and greatest() function from MySQL.

How do I find unique two column combinations in SQL?

Select with distinct on all columns of the first query. Select with distinct on multiple columns and order by clause. Count() function and select with distinct on multiple columns.

Can we apply distinct on multiple columns?

Multiple fields may also be added with DISTINCT clause. DISTINCT will eliminate those rows where all the selected fields are identical.

How can I get distinct values from two tables in MySQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.


1 Answers

$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);

Should produce query:

SELECT  DISTINCT id,agency_id, university_id FROM `tbl_universityagency` where agency_id=1 LIMIT 1
like image 141
Mihkel Viilveer Avatar answered Sep 27 '22 15:09

Mihkel Viilveer