Here is my table description:
CREATE TABLE my_table
(id int, name varchar(9), value varchar(13))
;
INSERT INTO my_table
(id, name, value)
VALUES
(0, ‘timezone’, ‘Europe/London’),
(0, ‘language’, ‘en’),
(0, ‘country’, ’45’),
(0, ‘something’, ‘x’),
(1, ‘timezone’, ‘Europe/Paris’),
(1, ‘language’, ‘fr’),
(1, ‘country’, ’46’)
;
id name value
0 timezone Europe/London
0 language en
0 country 45
0 something x
1 timezone Europe/Paris
1 language fr
1 country 46
My result should look
ID TIMEZONE LANGUAGE COUNTRY SOMETHING
0 Europe/London en 45 x
1 Europe/Paris fr 46 (null)
How can I execute this query in Zend Framework by using Zend_Db_Table or Zend_Db_Select? Thank you so much!
[EDIT Query from comments added]
SELECT CONCAT( 'SELECT table.id', GROUP_CONCAT(CONCAT(' , t_', REPLACE(name, '', ''), '`.value AS `', REPLACE(name, '`', ''), '' ) SEPARATOR ''), '
FROM table` ',
GROUP_CONCAT(CONCAT(' LEFT JOIN table AS t_', REPLACE(name, '', ''), '` ON `table`.id = `t_', REPLACE(name, '`', ''), '.id AND t_', REPLACE(name, '', '``'), '.name = ',
QUOTE(name) ) SEPARATOR ''), '
GROUP BY table.id' )
INTO @qry FROM (SELECT DISTINCT name FROM table) t;
PREPARE stmt FROM @qry;
EXECUTE stmt;
here’s a solution that uses Zend_Db_Adapter from a DbTable model:
I lack the SQL chops to parse your query and demonstrate the fluent interface. Good Luck.