如何使用 Zend_Db_Select 更改带有联接的查询中的列顺序

发布于 2024-11-05 00:57:09 字数 4025 浏览 1 评论 0原文

我有相当复杂的查询,我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-11-12 00:57:09

您可以使用 ->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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文