我在使用GROUP BYORDER BYLIMIT时遇到问题,通常我的查询工作正常,但是当我尝试变得更具体时,我总是遇到问题。我尝试将其分解并逐步进行,但我无法弄清楚问题。

从我的用户类中,我这样称呼数据库:

$this->_db->get('listings', array('userid', '=', $this->data()->id, ' AND ',
'email', ' = ', $this->data()->email, ' GROUP BY reference ORDER BY row ASC'));


我的数据库类的设置如下:

public function get($table, $where){return $this->action('SELECT * ', $table, $where);}


private function action($action, $table, $where = array()){
        $operators = array('=', '>', '<', '>=', '<=' , 'AND' ,'OR', 'LIKE', 'GROUP BY','ORDER BY', 'ASC', 'DESC');

        if(!empty($where)){
            $sql = "{$action} FROM {$table} WHERE ";
            if(count($where) > 3){
                $sql .= "(";
                $isOp = FALSE;
                foreach ($where as $key => $value) {
                    if($isOp){
                        if(in_array($value, $operators)){
                            $sql .= " {$value} ";
                            $isOp = FALSE;
                        }else{return FALSE;}
                    }else{
                        $sql .= " {$value} ";
                        $isOp = TRUE;
                    }
                }
                $sql .= ")";

                if(!$this->query($sql, array($value))->error()){return $this;}
            }else if(count($where) === 3){
                $field      = $where[0];
                $operator   = $where[1];
                $value      = $where[2];
                if(in_array($operator, $operators)){
                    $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; // NO $value ?
                    if(!$this->query($sql, array($value))->error()){return $this;}
                }
            }
        }else{
            // If array is empty
            $sql = "{$action} FROM {$table}";
            if(!$this->query($sql)->error()){return $this;}
        }
        return FALSE;
    }


public function query($sql, $params = array()){
        $this->_error = FALSE;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }// End IF count

            if($this->_query->execute()){
                    $this->_lastID = $this->_pdo->lastInsertId();
                    try{
                        $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                    }catch(Exception $e){
                        // Catch Error
                    }
                    $this->_count = $this->_query->rowCount();
            }else{$this->_error = TRUE;}
        }
        return $this;
    }


通常,我会通过阅读教程和按照书中的说明从头开始构建所有内容。

同样,我在以下分组方式,排序方式,限制方式等方面遇到了麻烦。

提前致谢。

我得到的错误


  SELECT * FROM列表WHERE(userid = 4 GROUP BY引用)异常“ PDOException”,消息为“ SQLSTATE [42000]”:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在第1行的'GROUP BY reference)'附近使用正确的语法

最佳答案

我会在您的代码(DB类)中摆脱de():

$sql .= "(";
$sql .= ")";


GROUP BY不应位于WHERE的()部分中。

在哪里(userid = 4 GROUP BY参考)

一定是:

WHERE ( userid = 4 ) GROUP BY reference;


或更好:

WHERE userid = 4 GROUP BY reference;


在SELECT中使用SUM,COUNT等时,可以使用GROUP BY函数。否则您不需要它。

关于php - 使用GROUP BY,ORDER BY的PDO查询构造,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30198215/

10-14 14:18
查看更多