【问题标题】:Cakephp3 -> How to use computed field within where conditionCakephp3 -> 如何在 where 条件下使用计算域
【发布时间】:2016-08-10 08:30:41
【问题描述】:

我们如何在 where 条件下使用计算域?

假设

        $query = $this->table1
            ->find('all')
            ->select(['total_count' => '(SELECT count(*) FROM table2 where table2.fid = table1.id)'])
            ->autoFields(true)
            ->contain([
                'table2', 'table3'
                ])
            ->where(['total_count >' 1]);

未找到列:1054 未知列“total_count”在哪里

有没有办法在 where 条件下使用计算字段,或者如何解决这个问题?

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    经过一番搜索找到了答案

    通过使用have,你可以在条件中使用计算域

    $query->having(['total_count >' => 0]);
    

    【讨论】:

    • 是的,但是用于分组查询,如果您想在 where 条件下使用计算字段,请查看this 回答是否有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多