如何在 MySQL 查询中使用 AND 和 OR?

How to use AND and OR in MySQL query?

我想获取那些 date_last_copied 字段为空或小于当前日期的记录。我试过了,但没有给我想要的结果:

$tasks = $this->Control->query("
    SELECT *
    FROM
        `controls`
    WHERE
        `owner_id` = ".$user_id."
        AND `control_frequency_id` = ".CONTROL_FREQUENCY_DAILY."
        OR `date_last_copied` = ''
        OR `date_last_copied` < ".  strtotime(Date('Y-m-d'))."
");

不确定你的整个逻辑,但你的最终查询语句应该是这样的:

SELECT * FROM `controls` WHERE (`owner_id` = <some owner_id>) 
AND (`control_frequency_id` = <some id value>)
AND (`date_last_copied` = '' OR 
     `date_last_copied` IS NULL OR 
     `date_last_copied` < CURDATE() )

小心使用括号以符合您的逻辑。

我认为当前查询看起来像这样。即查找 owner_id 和 frequency_id 正确的记录,其中 date_last_copied 为空或小于某个日期。这个逻辑对吗?

SELECT *
FROM controls
WHERE owner_id = ::owner_id::
    AND control_frequency_id = ::frequency_id::
    AND (
        date_last_copied IS NULL
        OR date_last_copied < ::date::
    )

但我们真的应该使用 CakePHP 查询生成器,而不是 运行 原始 SQL。 article 给出了一些细节。如果我要尝试一个解决方案,我们会想要类似下面的东西。但我们理想情况下希望 CakePHP 社区的某个人在这里插话。编辑:请注意,这似乎仅适用于 CakePHP 3.0。

// Build the query
$query = TableRegistry::get('controls')
    ->find()
    ->where([
        'owner_id' => $ownerId,
        'control_frequency_id' => $frequencyId,
        'OR' => [
            ['date_last_copied IS' => null],
            ['date_last_copied <' => $date]
        ]
    ]);
// To make sure the query is what we wanted
debug($query);
// To get all the results of the query
foreach ($query as $control) {
    . . .
}

我建议的是这个,而不是上面的原始 SQL 字符串,因为:

  1. 我们现在可以利用 CakePHP 的 ORM 模型。
  2. 我们不必担心 SQL 注入,您目前很容易受到这种攻击。

编辑:好的,这是对适用于 CakePHP 2.0 的语法的猜测... YMMV

$controls = $this->controls->find('all', [
    'conditions' => [
        'owner_id' => $ownerId,
        'control_frequency_id' => $frequencyId,
        'OR' => [
            ['date_last_copied IS' => null],
            ['date_last_copied <' => $date]
        ]
    ]
];

否则,我们只是将原始查询用作准备好的语句:

$result = $this->getDataSource()->fetchAll("
    SELECT *
    FROM controls
    WHERE owner_id = ?
        AND control_frequency_id = ?
        AND (
            date_last_copied IS NULL
            OR date_last_copied < ?
        )",
    [$ownerId, $frequencyId, $date]
);

始终指定您的应用程序使用的 cakePHP 版本。

对于 SQL AND 和 OR,此查询在 CakePHP 3.0 中应该可以正常工作。

$query = ModelName>find()
    ->where(['colunm' => 'condition'])
    ->orWhere(['colunm' => 'otherCondition'])
    ->andWhere([
        'colunm' => 'anotherContion',
        'view_count >' => 10
    ])
    ->orWhere(['colunm' => 'moreConditions']);