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

时间:2022-03-18 15:28:50

How to write below 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");

1 个解决方案

#1


You probably want to do a find 'all' on the TmpSale model and define the fields for the COUNT and SUM:-

您可能想在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);

I've included a debug($data) at the end as you'll probably want to take a look at the array returned as it will perhaps not be in the format you'd expect due to the COUNT and SUM fields.

我在最后包含了一个调试($ data),因为你可能想看一下返回的数组,因为它可能不是你想要的格式,因为COUNT和SUM字段。

#1


You probably want to do a find 'all' on the TmpSale model and define the fields for the COUNT and SUM:-

您可能想在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);

I've included a debug($data) at the end as you'll probably want to take a look at the array returned as it will perhaps not be in the format you'd expect due to the COUNT and SUM fields.

我在最后包含了一个调试($ data),因为你可能想看一下返回的数组,因为它可能不是你想要的格式,因为COUNT和SUM字段。