【问题标题】:Between query in cakephp 3.3cakephp 3.3 中的查询之间
【发布时间】:2017-03-31 11:56:47
【问题描述】:

查询正常

想用 cakephp 3.3 编写下面的查询

SELECT 

Books.id AS Books__id, Books.title AS Books__title, Books.for_min_age AS Books__for_min_age, Books.for_max_age AS Books__for_max_age, Books.for_min_grade AS Books__for_min_grade, Books.for_max_grade AS Books__for_max_grade 从 书籍书籍 在哪里 4 在 Books.for_min_age 和 Books.for_max_age 之间 或者 2 Books.for_min_grade AND Books.for_max_grade

【问题讨论】:

    标签: php mysql cakephp cakephp-3.0


    【解决方案1】:

    我想知道你是否甚至试图找到答案。如果是这种情况,您会发现 BETWEEN 不会按照您在此处描述的方式工作。阿法克。

    假设您的模型设置正确,以下代码在 CakePHP 中有效。

    $age = 4;
    $grade = 2;
    
    $query = TableRegistry::get('Books')->find('all')
        ->select([
            'id', 
            'title',
            'for_min_age',
            'for_max_age',
            'for_min_grade',
            'for_max_grade'
        ]);
    
    $query->where(function ($exp) use ($age, $grade) {
        $ageExp = $exp->and_(function ($and) use ($age) {
            return $and
                ->lte('for_min_age', $age)
                ->gte('for_max_age', $age);
        });
    
        $gradeExp = $exp->and_(function ($and) use ($grade) {
            return $and
                ->lte('for_min_grade', $grade)
                ->gte('for_max_grade', $grade);
        });
    
        return $exp->add(['OR' => [$ageExp, $gradeExp]]);
    });
    
    $data = $query->execute();
    

    这将导致:

    SELECT 
      Books.id AS `Books__id`, 
      Books.title AS `Books__title`, 
      Books. for_min_age AS `Books__for_min_age`,
      Books. for_max_age AS `Books__for_max_age`,
      Books. for_min_grade AS `Books__for_min_grade`,
      Books. for_max_grade AS `Books__for_max_grade` 
    FROM 
      books Books 
    WHERE 
      (
        (
          for_min_age <= 4 
          AND for_max_age >= 4
        ) 
        OR (
          for_min_grade <= 2 
          AND for_max_grade >= 2
        )
      )
    

    编辑

    理论上你可以这样做,但 CakePHP 会将 numbernumber2 转换为字符串,这是行不通的。

    $query->where(function ($exp) use ($age, $grade) {
        return $exp->or_(function ($or) use ($age) {
            return $or->between($age, 'for_min_age', 'for_max_age');
        })->between($grade, 'for_min_grade', 'for_max_grade');
    });
    

    【讨论】:

    • 这是你所要求的,也是你应得的。
    • 查询正常,你可以试试。我正在寻找 $value BETWEEN column1 AND coulmn2
    • 我更新了答案。这不是直接可能的。但是由于BETWEEN 只是我发布的查询的简写,所以你很高兴。要么接受,要么离开它。见这里:dev.mysql.com/doc/refman/5.7/en/…
    • 附加说明,表达value BETWEEN column1 AND column2 语法实际上是可能的,如果一个人(无论出于何种原因)真的想走这条路而不是将事情分解成简单的AND 比较:@ 987654322@
    【解决方案2】:

    我通过 -

    实现了这一点
     $this->find()->where([$age . ' BETWEEN Books.for_min_age AND Books.for_max_age OR ' . $grade . ' BETWEEN Books.for_min_grade AND Books.for_max_grade']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      相关资源
      最近更新 更多