如何在cakephp中使用大小写之和进行搜索

bvjveswy  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(126)

我想在cakephp中用或或条件在每个数据中搜索一个case的总计数生成的数据。

SELECT `Branches`.`name` AS `Branches__name` , `Branches`.`code` AS `Branches__code` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END )
) AS `carried` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =0
THEN 1
END )
) AS `unCarried`
FROM `branches` `Branches`
INNER JOIN `products_carried_per_branches` `prodCarried` ON ( prodCarried.company_id = Branches.company_id
AND prodCarried.branch_code = Branches.code )
WHERE (
`Branches`.`company_id` =200017
AND `Branches`.`deleted` =0
AND `prodCarried`.`deleted` =0
)
GROUP BY `code`
HAVING COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END ) =39

图片显示了没有代码

的mysql结果
虽然这是我的cakephp代码,但我想在cakephp或条件中实现having sql,以搜索生成的和计数或搜索数据名称和代码。这可能吗?不可能

$carriedCase = $query->newExpr()
        ->addCase(
            $query->newExpr()->add(['prodCarried.carried' => '1']),
            1,
            'integer'
        );
    $unCarriedCase = $query->newExpr()
        ->addCase(
            $query->newExpr()->add(['prodCarried.carried' => '0']),
            1,
            'integer'
        );
    //disctinct code
    $query ->select([
            'name',
            'code',
            'carried' => $query->func()->count($carriedCase),
            'unCarried' => $query->func()->count($unCarriedCase),
            'prodCarried.id',
            'prodCarried.validity_start',
            'prodCarried.validity_end',
    ]);
    $query ->distinct([
        'code'
    ]);
     $query->join([
      'prodCarried' => [
        'table' =>'products_carried_per_branches',
        'type' => 'inner',
        'conditions' => [
          'prodCarried.company_id = Branches.company_id',
          'prodCarried.branch_code = Branches.code',
        ]
      ]
    ]);

    $query->where(function (QueryExpression $exp, Query $q) use($option,$search){
        $exp->eq('Branches.company_id', $option['company_id']);
        $exp->eq('Branches.deleted', 0);
        $exp->eq('prodCarried.deleted', 0);
        if(!empty($search)){
            $orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
                $or->like('name', "%$search%");
                $or->like('code', "%$search%");
                //****************************

                return $or;
            });
            $exp->add($orConditions);
        }
    return $exp;
    });
4nkexdtk

4nkexdtk1#

根据您的DBMS,您可以引用选择列表的聚合:

$query->having([
    'carried' => $searchCount,
]);

否则,必须重新创建聚合:

$query->having(
    function (
        \Cake\Database\Expression\QueryExpression $exp,
        \Cake\ORM\Query $query
    ) use (
        $carriedCase,
        $searchCount
    ) {
        return $exp->eq(
            $query->func()->count($carriedCase),
            $searchCount
        );
    }
);

另请参阅

*Cookbook〉数据库访问和ORM〉查询生成器〉聚合-分组和拥有

相关问题