1

大家好,我想在 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%')
4

2 回答 2

2

只需将嵌套的 - 子句拆分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中 eachAND是一个单独的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%');

(注意:为了更好的可读性,我添加了手动换行符)

于 2013-09-19T08:22:52.687 回答
1

我认为跟随更好。NEST 将打开括号 UNNEST 将关闭括号

$where = new Sql\Where();
$where->NEST//start braket
->equalTo('col','someVal')
->OR
->equalTo('col','secondVal')
->UNNEST; //close bracet

这将产生 WHRE (col=someVal OR col=secondVal) 还要记住在终止后(放置 ;)如果 NEST 已经存在,它将自动添加 NUNEST 例如我认为以下更好。NEST 将打开括号 UNNEST 将关闭括号

$where = new Sql\Where();
$where->NEST//start braket
->equalTo('col','someVal')
->OR
->equalTo('col','secondVal');
$where->OR;
$where->NEST//start braket
->equalTo('col','someVal1')
->OR
->equalTo('col','secondVal2');

这将产生 WHRE (col=someVal OR col=secondVal) OR (col=someVal2 OR col=secondVal2)

于 2014-04-05T13:01:56.140 回答