Zend Framework:按两个字段选择不同的行

时间:2013-10-02 15:27:23

标签: mysql zend-framework zend-db-table

我有一个dbtable

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

我想要

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

尝试

$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:为什么Zend Framework这么难?!

3 个答案:

答案 0 :(得分:0)

试试这个:

Select 
field1,
field2
from mytable
group by field1, field2

答案 1 :(得分:0)

正如Sashi Kant所说,这可以由一个小组在field_1,field_2上完成。以下是如何使用Zend DB:

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

答案 2 :(得分:0)

您也可以使用Zend_Db_Expr

试试这个:

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