将复杂的查询转换为zend_db选择对象格式

时间:2012-11-20 11:43:10

标签: php mysql zend-framework

我正在使用ZEND,我无法将以下查询转换为zend_db选择对象格式:

SELECT *,CASE WHEN @score != score THEN @rank := @rank + 1 ELSE @rank END AS rank,
                @score := score AS dummy_value
                FROM (  SELECT score,username,ID,firstName,lastName
                FROM site_members, 
                    (SELECT @rank := 0, @score := NULL) AS vars 
                    WHERE `status` = 1 AND score > 0
                ORDER BY score DESC) AS h;
像这样:

    $select = $this -> db -> select();
    $select -> from('site_members', array('COUNT(*) AS count'));
    $select -> where("ID  = ?", $memberID, Zend_Db::INT_TYPE);
    $row = $this -> db -> fetchRow($select);

1 个答案:

答案 0 :(得分:1)

我处理了我的问题并找到了最佳答案:

            $inner_query = $this -> db -> select() -> from('site_members', array('score', 'username', 'ID', 'firstName', 'lastName')) 
                                 -> from(array("vars"=>new Zend_Db_Expr('(SELECT @rank := 0, @score := NULL)')))
                                 -> where("site_members.status = ?",1)
                                 -> where("score > 0")
                                 -> order(array("score DESC"));
            $select = $this -> db -> select();
            $select ->from(array("h"=>$inner_query),array(new Zend_Db_Expr('*'), 
                           new Zend_Db_Expr('CASE WHEN @score != score THEN @rank := @rank + 1 ELSE @rank END AS rank'), 
                           new Zend_Db_Expr('@score := score AS dummy_value')));