У меня довольно сложный запрос, и я могу построить его с помощью 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 несколько раз (он генерирует новое объединение в этом случае, которое не то, что я хочу), чтобы изменитьпорядок полей электронной почты, имени и фамилии в запросе.У вас есть идеи, как решить эту проблему?