php - 使用 GROUP BY 、 ORDER BY 构建 PDO 查询

标签 php mysql pdo

我在使用GROUP BYORDER BYLIMIT时遇到问题,一般来说,我的查询工作正常,但是当我尝试获取更多信息时具体来说,我不断遇到问题。我尝试将其分解并一步一步进行,但我无法找出问题所在。

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

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

我的DB类设置如下:

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

总的来说,我通过阅读教程并遵循书籍中的说明从头开始构建一切。

我再次遇到了以下 Group By、Order By、Like、Limit 的问题。我可能没有正确绑定(bind)它,但我没有足够的知识来发现我的错误。

提前致谢。

我收到错误

SELECT * FROM listings WHERE ( userid = 4 GROUP BY reference )exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY reference )' at line 1

最佳答案

我会在你的代码(DB类)中去掉de():

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

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

WHERE(用户 ID = 4 按引用分组)

必须是:

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/

相关文章:

MySQL基于替代列的列查询

php - 如何在PDO中将多个查询的结果添加到数组中?

php - 类内 PDO 合并

php - 从 PHP 到 PHPMYADMIN 的 DOB 集合

php - MySQL/PHP/PDO + 如何为 IN() 子句中的每个(重复)条目获取一行?

php - 从生成的复选框中将多个条目插入数据库

php - PDO MySQL 之类的查询不返回值

php - Kohana 2.3.4 ORM - 删除数据透视表关系

php - 使用 php 解析器改进和简化 css

php - 显示第一个子句值,然后显示第二个子句值