如何使用 Zend_Db_Select 更改带有联接的查询中的列顺序
我有相当复杂的查询,我可以使用 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 多次(在这种情况下它将生成新的联接,这不是我想要的)来更改查询中电子邮件、名字和姓氏字段的顺序。您对如何解决这个问题有什么想法吗?
I have rather complex query and I could build it with Zend_Db_Select step by step. The query look likes this:
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
But now I need to be able to dynamically change the order of columns, to be able transform aforementioned queries to something like:
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
Or for example, something like this one:
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
The problem is that I can't use method from of Zend_Db_Select multiple times (it will generate new join in this case which is not what I want) to change the order of email, first and last names fields in the query. Do you have any ideas how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
->columns()
方法来选择所需的列。在 from 方法中使用空数组,然后使用 columns() 调用,即使内部有一个大的 Zend_Db_Expr,您也可以决定按照您想要的方式和顺序编写选择参数。
You can use the
->columns()
method to choose the columns you want.Us empty arrays in the from methods, then use columns() calls, even with a big Zend_Db_Expr inside, where you can decide to write your select arguments in the way and order you want.