【问题标题】:lithium framework how to make a query with OR锂框架如何使用 OR 进行查询
【发布时间】:2014-07-31 16:43:09
【问题描述】:

我开始使用锂 PHP 框架。我必须进行查询以获取在“标题”字段中有“%test%”或“%easy%”的问题。 我尝试使用以下代码:

$questions = Questions::find('all', array(
        'conditions'    => array(
            'name' => array(
                'like' => array(
                    '%easy%',
                    '%test%'
                )
            )
        )
    ));

但它会进行以下查询:

SELECT * FROM `questions` AS `Questions` WHERE (`name` LIKE '%easy%' AND `name` LIKE '%test%')

如何用 OR 代替 AND?

【问题讨论】:

    标签: php mysql lithium


    【解决方案1】:

    作为您问题的解决方案,您可以在 (=) 中使用而不是喜欢

    'conditions'    => array(
        'name' => array(
            '=' => array(
                '%easy%',
                '%test%'
            )
        )
    )
    

    这将生成查询:

    WHERE ((`name` IN ('%easy%', '%test%')))
    

    或者可以在使用两个不同的字段进行搜索时使用:

    'conditions' => array(
        'OR' => array(
            'name' => array(
                '=' => array(
                    '%easy%',
                    '%test%'
                )
            ),
            'name2' => array(
                '!=' => array(
                    '%easy%',
                    '%test%'
                )
            )
        )
    ),
    

    这将生成查询:

    WHERE ((`name` IN ('%easy%', '%test%')) OR (`name2` IN ('%easy%', '%test%')))
    

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 2019-04-04
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多