【问题标题】:cakephp find multiple sets of OR in conditionscakephp 在条件下查找多组 OR
【发布时间】:2015-02-10 19:04:01
【问题描述】:

我看到另一个问题在技术上回答了我正在回答的问题,但我的查询不起作用。我认为查询是不言自明的,但它没有返回任何行。

CakePHP find conditions with multiple 'OR'

$sector_info['Notam'] = $this->Notam->find('all', array(
                    'group' => 'Notam.id',
                    'conditions' => array(
                        'AND' => array(
                            'OR' => array(
                                'Notam.notam_expire_dtg < '.date("YmdHis"),
                                'Notam.notam_expire_dtg' => '',
                                ),
                            'OR' => array(
                                'Notam.account_id' => $sector_info['Airport']['identifier'],
                                'Notam.cns_location_id' => $sector_info['Airport']['identifier'],
                                'Notam.account_id' => $sector_info['Airport']['icao'],
                                'Notam.cns_location_id' => $sector_info['Airport']['icao'],
                                ),
                            ),
                        ),
                    )
                );

我想提取(未过期或未过期)AND(或其他 4 个条件。

在此先感谢您, 瑞恩

编辑:这是输出的查询,根本没有提到 Notam.notam_expire_dtg:

SELECT `Notam`.`id`, `Notam`.`key`, `Notam`.`source_id`, `Notam`.`account_id`, `Notam`.`notam_id`, `Notam`.`notam_nrc`, `Notam`.`xoveraccountid`, `Notam`.`xovernotamid`, `Notam`.`notam_part`, `Notam`.`cns_location_id`, `Notam`.`icao_id`, `Notam`.`icao_name`, `Notam`.`total_parts`, `Notam`.`notam_effective_dtg`, `Notam`.`notam_expire_dtg`, `Notam`.`notam_lastmod_dtg`, `Notam`.`notam_text`, `Notam`.`notam_report`, `Notam`.`notam_qcode`, `Notam`.`pull`, `Notam`.`dflag`, `Notam`.`sql_update` FROM `aaid_cake`.`notams` AS `Notam` WHERE ((`Notam`.`account_id` IN ('PABE', 'BET')) OR (`Notam`.`cns_location_id` IN ('PABE', 'BET'))) GROUP BY `Notam`.`id`

【问题讨论】:

  • date("YmdHis") 还是 date("Y-m-d H:i:s") ? 'Notam.notam_expire_dtg 'date("YmdHis"),
  • 日期组件是正确的,我们没有使用 mysql 日期格式,我们使用的是数据库中每 5 分钟接收一次的格式 'YmdHis' 在我添加两个 'OR' 之前查询有效'AND' 我在 'OR' 之外有日期元素,但这不包括没有值的行 ''
  • 'Notam.id' 是唯一 id 吗?
  • 是的,从另一个加入了一堆表并有欺骗性的查询中复制而来。我也会删除它,谢谢

标签: mysql cakephp


【解决方案1】:

php 基础知识:您不能拥有重复的数组键,您的第二个 ['OR'] 声明将覆盖第一个。这应该会让你的错误显而易见。

试试这个:

'conditions' => [
    'AND' => [
        ['OR' => [/*...*/]],
        ['OR' => [/*.../*]]
    ]
 ]

【讨论】:

  • 太棒了!午饭后我会试试的。谢谢!
【解决方案2】:

你可以试试这个:

$sector_info['Notam'] = $this->Notam->find('all', array(
                'group' => 'Notam.id',
                'conditions' => array(
                    'OR' => array(
                            'Notam.notam_expire_dtg < '.date("YmdHis"),
                            'Notam.notam_expire_dtg' => '',
                     ),
                    'AND'=>array(
                     'OR' => array(
                            'Notam.account_id' => $sector_info['Airport']['identifier'],
                            'Notam.cns_location_id' => $sector_info['Airport']['identifier'],
                            'Notam.account_id' => $sector_info['Airport']['icao'],
                            'Notam.cns_location_id' => $sector_info['Airport']['icao'],
                            )
                        ),                            
                    ),
                )
            );

【讨论】:

  • 您帖子上方帖子中的小绿色复选标记表示该问题已得到准确回答。
  • 但是你说你要试试……所以我给了你完整的答案。即使您不需要答案,也非常感谢
  • 感谢您的帮助,我真的希望我们在这里有一个更大的 cakephp 社区。我不是故意让你失望
猜你喜欢
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多