我正在使用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);

最佳答案

我仔细研究我的问题,找到了最好的答案:

            $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')));

关于php - 将复杂的查询转换为zend_db选择对象格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13472461/

10-11 04:08