zend -mysql使用一个字段值的值到另一个字段名[编辑]

时间:2012-12-26 12:00:36

标签: mysql zend-framework

这是我的表格描述:
CREATE TABLE my_table     (id int,name varchar(9),value varchar(13)) ;

INSERT INTO my_table     (idnamevalue) VALUES     (0,'时区','欧洲/伦敦'),     (0,'language','en'),     (0,'country','45'),     (0,'某事','x'),     (1,'时区','欧洲/巴黎'),     (1,'语言','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_Db_Table或Zend_Db_Select在Zend Framework中执行此查询?非常感谢你!

[添加评论的编辑查询]

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; 

1 个答案:

答案 0 :(得分: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 chops来解析您的查询并演示流畅的界面。祝你好运。