簡體   English   中英

鋰框架如何使用 OR 進行查詢

[英]lithium framework how to make a query with OR

我開始使用鋰 PHP 框架。 我必須進行查詢以獲取在“title”字段中包含“%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?

作為您問題的解決方案,您可以在 (=) 中使用,而不是像

'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%')))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM