user1890184
user1890184

Reputation: 49

Zend Framework: select distinct rows by two fields

I have in a dbtable

field 1   field 2
1         2
2         3
2         3
2         2
1         1
2         2

I want to get

field 1 field 2
1       2
2       3
2       2
1       1

Tried

$select = $this->_dbTable->select()->from($this->_dbTable,array("DISTINCT(field_1) as field_1","DISTINCT(field_2) as field_2"));
$select = $this->_dbTable->select()->from($this->_dbTable,array("DISTINCT(field_1, field_2) as field_1, field_2"));

PS: Why this Zend Framework's so hard?!

Upvotes: 0

Views: 4257

Answers (3)

Dapter20
Dapter20

Reputation: 353

You can use Zend_Db_Expr too

Try this:

$select = $this->_dbTable->select()->from($this->_dbTable, new Zend_Db_Expr('DISTINCT(field_1) as field_1'));

Upvotes: 0

Fouad Fodail
Fouad Fodail

Reputation: 2643

As Sashi Kant suggested this can be done by a group by on field_1, field_2. Here is how to make it using Zend DB :

$select = $this->_dbTable->select()->from($this->_dbTable, array("field_1", "field_2"))
                                   ->group(array("field_1", "field_2"));

Upvotes: 1

Sashi Kant
Sashi Kant

Reputation: 13465

Try this :

Select 
field1,
field2
from mytable
group by field1, field2

Upvotes: 0

Related Questions