只有两个AND语句中的第二个正在为我阅读。我相信我遇到了一个嵌套问题,但目前还不确定:
...'conditions' => array(
"AND" => array(
array($ApplicantAge . ' BETWEEN Age.Min_Age AND Age.Max_age'),
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount)
),
"AND" => array(
array($PsSpouseAge . ' BETWEEN Age.Min_Age AND Age.Max_age'),
'PlanDetail.company_id' => '27',
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount)
)
),...
正如您所看到的,我正在尝试获取所有记录 $ ApplicantAge。 'BETWEEN Age.Min_Age和Age.Max_age AND ONLY特定记录 WHERE'PlanDetail.company_id'=> '27 和 $ PsSpouseAge。 'BETWEEN Age.Min_Age和Age.Max_age'), ......
这是我的SQL输出:
WHERE ((18 BETWEEN `Age`.`Min_Age` AND `Age`.`Max_age`) OR (`Zips`.`title` = '98605') OR (`Applicant`.`amount` IN ('as', '2'))) AND ((24 BETWEEN `Age`.`Min_Age` AND `Age`.`Max_age`) AND (`PlanDetail`.`company_id` = 27) AND (`Zips`.`title` = '97378') AND (`Applicant`.`amount` IN ('as', '2')))
目前,'PlanDetail.company_id'=> '27',是重写,只有满足此'PlanDetail.company_id'=>的结果'27'标准正在显示
我已经有一段时间了,所以它的所有内容都开始在眼睛视图中混合..感谢您注意我的SQL逻辑有任何问题。
以下是我的完整加入和条件:
$options = array(
'joins' => array(
array(
'table' => 'plans_zips',
'alias' => 'PZips',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array('Plan.id = PZips.plan_id')
),
array(
'table' => 'zips',
'alias' => 'Zips',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array('Zips.id = PZips.zip_id')
)
),
'conditions' => array(
'OR' => array(
'AND' => array(
array($ApplicantAge . ' BETWEEN Age.Min_Age AND Age.Max_age'),
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
'NOT' => array(
'PlanDetail.company_id' => 27
)
),
array($PsSpouseAge . ' BETWEEN Age.Min_Age AND Age.Max_age')
)
),
答案 0 :(得分:3)
数组键是唯一的,您不能拥有两个"AND"
键。这种情况是covered in the manual,只需将它们包装在另一个数组中:
array(
array('and' => array(…)),
array('and' => array(…))
)
由于条件默认为AND
,但您可以将其完全删除。 (a AND b AND c) AND (d AND e AND f)
与a AND b AND c AND d AND e AND f
相同。
在我看来,您发布的条件可以简化为:
'conditions' => array(
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
'PlanDetail.company_id' => 27,
"$ApplicantAge BETWEEN Age.Min_Age AND Age.Max_age",
"$PsSpouseAge BETWEEN Age.Min_Age AND Age.Max_age"
)
显然你想要这样的东西:
'conditions' => array(
'or' => array(
'and' => array(
"$ApplicantAge BETWEEN Age.Min_Age AND Age.Max_age",
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
'not' => array(
'PlanDetail.company_id' => 27
)
),
"$PsSpouseAge BETWEEN Age.Min_Age AND Age.Max_age"
)
)
答案 1 :(得分:0)
我能够以这种方式工作(并通过添加第二个查询来增强):
'conditions' => array(
"OR" => array(
'AND' => array(
$ApplicantAge . ' BETWEEN Age.Min_Age AND Age.Max_age',
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
"NOT" =>
array(array('PlanDetail.company_id' => array('27','3')))),
array(
$PsSpouseAge . ' BETWEEN Age.Min_Age AND Age.Max_age',
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
'PlanDetail.company_id' => '27'),
array(
$OdsSpouseAge . ' BETWEEN Age.Min_Age AND Age.Max_age',
'Zips.title' => $Zip,
'Applicant.amount' => array($comboType, $memberCount),
'PlanDetail.company_id' => '3'))
),
结果SQL是:
WHERE
((((18 BETWEEN `Age`.`Min_Age` AND
`Age`.`Max_age`) AND
(`Zips`.`title` = '98258') AND
(`Applicant`.`amount` IN ('as', '2')) AND
(NOT (`PlanDetail`.`company_id` IN (27, 3)))))
OR
(((50 BETWEEN `Age`.`Min_Age` AND
`Age`.`Max_age`) AND
(`Zips`.`title` = '98258') AND
(`Applicant`.`amount` IN ('as', '2')) AND
(`PlanDetail`.`company_id` = 27)))
OR
(((50 BETWEEN `Age`.`Min_Age`AND
`Age`.`Max_age`) AND
(`Zips`.`title` = '98258') AND
(`Applicant`.`amount` IN ('as', '2')) AND
(`PlanDetail`.`company_id` = 3))))
我愿意为此提出任何建议的优化建议。但这是我能让它发挥作用的唯一方法。