zend_db_table如何选择特定列的所有不同值

时间:2010-12-16 11:00:05

标签: php sql zend-db-table

大家好我相信这可能会变得微不足道,但我有以下代码

        $response = $groupsmapper->getDbTable()->fetchAll(
        $groupsmapper->getDbTable()->select('group_area_residence')
            ->distinct()

应该让我得到所有不同的group_area_residence。但是,它会获取该组的所有列。

我正在使用zend_db_table btw。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

根据Zend / Db / Table / Abstract.php中的select(),它检查是否包含from部分,而不是获取字段名称

 /**  
 * Returns an instance of a Zend_Db_Table_Select object.
 *
 * @param bool $withFromPart Whether or not to include the from part of the select based on the table
 * @return Zend_Db_Table_Select
 */
public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART)
{    
    require_once 'Zend/Db/Table/Select.php';
    $select = new Zend_Db_Table_Select($this);
    if ($withFromPart == self::SELECT_WITH_FROM_PART) {
        $select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
    }    
    return $select;
}

查看以下代码段是否有帮助(将table_name替换为所需的代码)

$select = $groupsmapper->getDbTable()
                       ->select()
                       ->distinct()
                       ->from(array('table_name'), array('group_area_residence'));

$response = $groupsmapper->getDbTable()->fetchAll($select);