如何使用 find 在 cakephp 中编写 distinct 和 count 查询

how to write distinct and count query in cakephp using find

如何使用 find 在 cakephp 中编写以下查询。

$this->tmp_sale->query("select  DISTINCT COUNT( tax ) as 'tax_amount' , SUM( quantity ) as 'quantity1',tax,tax_amount1 from tmp_sale where employee='$emp' and no ='$no' and store='$store_name' group by tax");

您可能想在 TmpSale 模型上查找 'all' 并定义 COUNT 和 SUM 的字段:-

$data = $this->TmpSale->find(
    'all',
    array(
        'fields' => array(
            'COUNT(tax) AS tax_amount',
            'SUM(quantity) AS quantity1',
            'tax',
            'tax_amount1'
        ),
        'conditions' => array(
            'employee' => $emp,
            'no' => $no,
            'store' => $store_name
        ),
        'group' => array(
            'tax'
        )
    )
);
debug($data);

我在最后包含了一个 debug($data),因为您可能想查看返回的数组,因为由于 COUNT 和SUM 字段。