大家好,我想在zend db(zf1.9)上构建此查询
SELECT `p`.*, `r`.`name` AS `retailer`, `placer`.`name` AS `placer_name`, `placer`.`contact_firstname` AS `placer_firstname`, `placer`.`contact_lastname` AS `placer_lastname`, `placer`.`role_id` AS `placer_role_id`, `editor`.`name` AS `editor_name`, `editor`.`contact_firstname` AS `editor_firstname`, `editor`.`contact_lastname` AS `editor_lastname`, `editor`.`role_id` AS `editor_role_id` FROM `pos` AS `p`
LEFT JOIN `retailers` AS `r` ON r.id = p.retailer_id
LEFT JOIN `members` AS `placer` ON placer.id = p.placer_id
LEFT JOIN `members` AS `editor` ON editor.id = p.editor_id WHERE (p.designer_id=116) AND ((placer.name LIKE '%demo%') OR (placer.contact_firstname LIKE '%demo%') OR (placer.contact_lastname LIKE '%demo%') OR (r.name LIKE '%demo%')) AND (`r`.`name` LIKE '%M%') ORDER BY `p`.`id` asc
我该怎么办?我的问题仅仅是
WHERE (p.designer_id=116) AND ((placer.name LIKE '%demo%') OR (placer.contact_firstname LIKE '%demo%') OR (placer.contact_lastname LIKE '%demo%') OR (r.name LIKE '%demo%')) AND (`r`.`name` LIKE '%M%')
最佳答案
只需将嵌套的where
子句拆分为多个and
语句(带有嵌套的or
):
第一:
(p.designer_id=116)
第二:
((placer.name LIKE '%demo%') OR (placer.contact_firstname LIKE '%demo%') OR (placer.contact_lastname LIKE '%demo%') OR (r.name LIKE '%demo%'))
第三:
(`r`.`name` LIKE '%M%')
在ZEND中,每个
AND
都是一个单独的where
子句,可能看起来像这样(未经测试!只是为了更好地理解!):->where("p.designer_id=116");
->where("(placer.name LIKE ?)
OR (placer.contact_firstname LIKE ?)
OR (placer.contact_lastname LIKE ?)
OR (r.name LIKE ?)", '%demo%', '%demo%', '%demo%', '%demo%');
->where("r.name LIKE ?", '%M%');
(注意:我添加了手动换行符以提高可读性)
关于mysql - 如何在zend db上的何处构建嵌套,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18881230/