Question

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?!

Was it helpful?

Solution

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

OTHER TIPS

Try this :

Select 
field1,
field2
from mytable
group by field1, field2

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'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top