CakePHP选择额外的列

CakePHP选择额外的列,cakephp,distinct,Cakephp,Distinct,我正在用CakePHP做一个项目,但遇到了一个奇怪的问题。我正在写一个查询: $brands = $this->Product->find('all', array( 'fields'=> array('DISTINCT Product.brand as brand'), 'order'=>'Product.brand ASC', 'conditions'=> array('Produc

我正在用CakePHP做一个项目,但遇到了一个奇怪的问题。我正在写一个查询:

    $brands = $this->Product->find('all', array(
            'fields'=> array('DISTINCT Product.brand as brand'),
            'order'=>'Product.brand ASC',
            'conditions'=> array('Product.subcategory_id'=>$subcategory_id)
    ));
它正在挑选我不想要的Product.id和Product.brand

它生成的查询是:

SELECT DISTINCT `Product`.`brand`, `Product`.`id` FROM `ecom`.`products` AS `Product` LEFT JOIN `ecom`.`subcategories` AS `SubCategory` ON (`Product`.`subcategory_id` = `SubCategory`.`id`) LEFT JOIN `ecom`.`categories` AS `Category` ON (`Product`.`category_id` = `Category`.`id`) WHERE `Product`.`subcategory_id` = 13 ORDER BY `Product`.`brand` ASC
如何从选择中跳过Product.id


感谢添加组解决了我的问题:

    $brands = $this->Product->find('all', array(
            'fields'=> array('DISTINCT Product.brand as brand'),
            'order'=>'Product.brand ASC',
            'conditions'=> array('Product.subcategory_id'=>$subcategory_id),
            'group' => 'brand'
    ));

我认为这是因为cake需要id来创建连接

试一试


你看到了吗?谢谢天行者,我找不到threadGoogle是你的朋友;)
$brands = $this->Product->find('all', array(
        'fields'=> array('DISTINCT Product.brand as brand'),
        'order'=>'Product.brand ASC',
        'conditions'=> array('Product.subcategory_id'=> $subcategory_id),
        'recursive' => -1
));