mysql - mysql中where子句中计算字段的用法

标签 mysql yii2

如果有人帮助我,请先感谢您。

这是根据不同参数过滤某些客户的查询。 在最后一个过滤器中,我需要根据查询中的计算字段余额过滤客户,当我尝试在 where 子句中使用计算字段时,它显示未知字段的错误,我应该做什么?

$sql = <<<SQL

        SELECT c.*, t.name as town_name, r.name as region_name, IFNULL((
            select sum(p.remaining) 
            from payments p
            left join sales s
            on s.payment_id = p.id
            where s.customer_id = c.id and s.status = 1
        ), 0) as credit_sales, 
        (
            IFNULL((
                SELECT sum(cl.amount)
                FROM customer_ledgers cl
                WHERE cl.customer_id = c.id and cl.status = 1 and cl.type = 1
            ), 0) 
            -
            IFNULL((
                SELECT sum(amount)
                FROM customer_ledgers
                WHERE customer_id = c.id and status=1 and type = 2
            ), 0)
        ) as balancex, (
          IFNULL( (
            SELECT `amount`
            FROM `customer_ledgers`
            WHERE customer_id = c.id and status = 1
            ORDER BY `date` DESC 
            LIMIT 1
          ), 0)
        ) as last_amount




        from customers c
        left join regions r
        on r.id = c.region_id
        left join towns t
        on  t.id = c.town_id

SQL

    # we need where?
    $sql .= 'WHERE c.status = 1';
    if (isset($params['CustomerSearch'])) {
        $sql .= " and ";
    }

    # add where statements to the query

    if (isset($params['CustomerSearch'])) {
            $isname     = isset($params['CustomerSearch']['name']);
            $istown     = isset($params['CustomerSearch']['region_name']);
            $isregion   = isset($params['CustomerSearch']['town_name']);
            $isbalance   = isset($params['CustomerSearch']['balancex']);

        if (isset($params['CustomerSearch']['name'])) {
            $this->name = trim($params['CustomerSearch']['name']);
            $sql .= " c.name like '%{$this->name}%'";
        }

        if (isset($params['CustomerSearch']['region_name']) && $params['CustomerSearch']['region_name'] !== '') {
            $this->region_name = trim($params['CustomerSearch']['region_name']);

            if ($isname) {
                $sql .= " and ";
            }
            $sql .= " r.id=" . $params['CustomerSearch']['region_name'];
        }

        if (isset($params['CustomerSearch']['town_name']) && $params['CustomerSearch']['town_name'] !== '') {
               $this->town_name = trim($params['CustomerSearch']['town_name']);

               if ($isname || $isregion) {
                   $sql .= " AND t.id = {$this->town_name}";
               }
           } 

        if (isset($params['CustomerSearch']['balancex']) && $params['CustomerSearch']['balancex'] !== '') {
               $this->balancex = trim($params['CustomerSearch']['balancex']);

               if ($isbalance) {
                   $sql .= " AND balancex > {$isbalance}";
               }
           }   
    }

    # town where clause.




    $result = Yii::$app->db->createCommand($sql)->queryAll();


    return $result;

最佳答案

您不能在 WHERE 子句中使用计算字段。您需要使用 HAVING 子句。

https://stackoverflow.com/a/16068737/4338862 .

关于mysql - mysql中where子句中计算字段的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282689/

相关文章:

php - Mysql 需要很长时间来插入和更新 Yii Framework

php - Yii2 依赖注入(inject)示例

configuration - Yii2:无法执行RBAC迁移(您应该配置 “authManager”…)

MySQL - 仅选择最后修改的(或 NULL 值)

MySQL 错误 : #1142 - SELECT command denied to user

mysql - 添加两列的简单 SQL 测试

php - mysql 数据导入 Dojo datagrid 表

php - 在 Yii2 Rest Api 中使用 Yii2-user 进行用户注册

php - 在 Yii2 中执行原始 SQL 查询?

mysql - 如何在 MySQL 中随机连接表?