我有相当复杂的查询,我可以逐步使用Zend_Db_Select构建它。查询看起来像这样:
SELECT `subscribers`.`subscriber_id`, `subscribers`.`email_address`, `subscribers`.`first_name`,
`subscribers`.`last_name`, `t1`.`value` AS `'SingleLineField'`, `t2`.`value` AS `'MultilineField'`,
`t3`.`value` AS `'CheckboxField'`
FROM `subscribers`
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS `s` ON subscribers.subscriber_id = s.subscriber_id
LEFT JOIN `subscribers_custom_data_string` AS `t1` ON subscribers.subscriber_id=t1.subscriber_id AND t1.field_id=177
LEFT JOIN `subscribers_custom_data_string` AS `t2` ON subscribers.subscriber_id=t2.subscriber_id AND t2.field_id=178
LEFT JOIN (SELECT subscribers_multivalued.subscriber_id, field_id, GROUP_CONCAT(value SEPARATOR ',') AS value
FROM subscribers_multivalued
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS s1 ON subscribers_multivalued.subscriber_id=s1.subscriber_id
WHERE field_id=179
GROUP BY subscribers_multivalued.subscriber_id, field_id) AS `t3` ON subscribers.subscriber_id=t3.subscriber_id AND t3.field_id=179
但现在我需要能够动态更改列的顺序,以便能够将上述查询转换为以下内容:
SELECT `subscribers`.`subscriber_id`, `subscribers`.`email_address` , `t1`.`value` AS `'SingleLineField'`, `t2`.`value` AS `'MultilineField'`,
`t3`.`value` AS `'CheckboxField'`, `subscribers`.`first_name`,
`subscribers`.`last_name`
FROM `subscribers`
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS `s` ON subscribers.subscriber_id = s.subscriber_id
LEFT JOIN `subscribers_custom_data_string` AS `t1` ON subscribers.subscriber_id=t1.subscriber_id AND t1.field_id=177
LEFT JOIN `subscribers_custom_data_string` AS `t2` ON subscribers.subscriber_id=t2.subscriber_id AND t2.field_id=178
LEFT JOIN (SELECT subscribers_multivalued.subscriber_id, field_id, GROUP_CONCAT(value SEPARATOR ',') AS value
FROM subscribers_multivalued
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS s1 ON subscribers_multivalued.subscriber_id=s1.subscriber_id
WHERE field_id=179
GROUP BY subscribers_multivalued.subscriber_id, field_id) AS `t3` ON subscribers.subscriber_id=t3.subscriber_id AND t3.field_id=179
或者例如,像这样的东西:
SELECT `subscribers`.`subscriber_id`, `t1`.`value` AS `'SingleLineField'`, `t2`.`value` AS `'MultilineField'`,
`t3`.`value` AS `'CheckboxField'`, `subscribers`.`email_address`, `subscribers`.`first_name`,
`subscribers`.`last_name`
FROM `subscribers`
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS `s` ON subscribers.subscriber_id = s.subscriber_id
LEFT JOIN `subscribers_custom_data_string` AS `t1` ON subscribers.subscriber_id=t1.subscriber_id AND t1.field_id=177
LEFT JOIN `subscribers_custom_data_string` AS `t2` ON subscribers.subscriber_id=t2.subscriber_id AND t2.field_id=178
LEFT JOIN (SELECT subscribers_multivalued.subscriber_id, field_id, GROUP_CONCAT(value SEPARATOR ',') AS value
FROM subscribers_multivalued
INNER JOIN (SELECT subscriber_id
FROM subscribers
WHERE list_id =68 AND state=1
LIMIT 0, 25) AS s1 ON subscribers_multivalued.subscriber_id=s1.subscriber_id
WHERE field_id=179
GROUP BY subscribers_multivalued.subscriber_id, field_id) AS `t3` ON subscribers.subscriber_id=t3.subscriber_id AND t3.field_id=179
问题是我不能多次使用Zend_Db_Select的方法(在这种情况下会产生新的连接,这不是我想要的)来改变查询中的电子邮件,名字和姓氏字段的顺序。你对如何解决这个问题有任何想法吗?
答案 0 :(得分:2)
您可以使用->columns()
方法选择所需的列。
我们在from方法中清空数组,然后使用columns()调用,即使里面有一个大的Zend_Db_Expr,你可以决定以你想要的方式和顺序编写你的选择参数。