0

这是我的表描述:
CREATE TABLE my_table ( idint, namevarchar(9), valuevarchar(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

我的结果应该看起来

  ID     TIMEZONE       LANGUAGE      COUNTRY SOMETHING
  0      Europe/London  en            45      x
  1      Europe/Paris   fr            46      (null)

如何在 Zend Framework 中使用 Zend_Db_Table 或 Zend_Db_Select 执行此查询?太感谢了!

[添加评论中的编辑查询]

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; 
4

1 回答 1

0

这是一个使用来自 DbTable 模型的Zend_Db_Adapter的解决方案:

/*accepts a string of SQL*/
public function getQuery($sql) {
    $query = $this->getAdapter->quoteInto($sql);
    $result = $this->getAdapter()->fetchAll($query);
    /*if you want the result as an array instead of an object: $result->toArray()*/
    return $result;
}

我缺乏分析查询和演示流畅界面的 SQL 代码。祝你好运。

于 2012-12-26T13:30:35.733 回答