php - SQLSTATE[42000] 聚合函数错误

标签 php mysql sql syntax-error

您好,我在 SQL 查询中遇到错误,但无法找出问题所在。这是迄今为止在 Barmar 帮助下的查询。

 $query = "SELECT c.*, count(s.curso_id) as count, SUM(IF(s.status = 'aprobado', 1, 0)) AS count_approved , SUM(IF(s.status = 'cupolleno', 1, 0)) AS count_cupolleno
    , SUM(IF(s.status = 'cancelado', 1, 0)) AS count_cancelado, SUM(IF(s.status = 'noacion', 1, 0)) AS count_noacion, SUM(IF(s.status = 'ama_de_casa', 1, 0)) AS count_ama_de_casa
    , SUM(IF(s.status = 'cliente_externo', 1, 0)) AS count_cliente_externo
    FROM cursos_modulos AS c
    LEFT JOIN subscriptions AS s ON s.curso_id = c.id
    LEFT JOIN users AS u ON u.userID = s.user_id GROUP BY c.id WHERE 1";
                if (!empty($id)) { $query .= " AND c.id = '$id'"; }
                if (!empty($ciudad)) { $query .= " AND c.ciudad = '$ciudad'"; }
                if (!empty($tipo)) { $query .= " AND c.tipo = '$tipo'"; }
                if (!empty($titulo))  { $query .=" AND c.titulo = '$titulo'"; }
                if (!empty($status))  { $query .= " AND c.status = '$status'"; }
 $paginate = new pagination($page, $query, $options);

我收到的错误消息如下:

Fatal error: Uncaught 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 'WHERE 1 AND c.id = '1' LIMIT 0, 30' at line 6' in E:\xampp\htdocs\admin\class\pagination.php:376 Stack trace: #0 E:\xampp\htdocs\admin\class\pagination.php(376): PDOStatement->execute() #1 E:\xampp\htdocs\admin\class\pagination.php(202): pagination->excecute_query() #2 E:\xampp\htdocs\admin\class\pagination.php(162): pagination->run(1, 'SELECT c., cou...', Array) #3 E:\xampp\htdocs\admin\search.php(146): pagination->__construct(1, 'SELECT c., cou...', Array) #4 {main} thrown in E:\xampp\htdocs\admin\class\pagination.php on line 376

最佳答案

group by 子句应位于 where 子句之后。即:

$query = "SELECT c.*, count(s.curso_id) as count, SUM(IF(s.status = 'aprobado', 1, 0)) AS count_approved , SUM(IF(s.status = 'cupolleno', 1, 0)) AS count_cupolleno
, SUM(IF(s.status = 'cancelado', 1, 0)) AS count_cancelado, SUM(IF(s.status = 'noacion', 1, 0)) AS count_noacion, SUM(IF(s.status = 'ama_de_casa', 1, 0)) AS count_ama_de_casa
, SUM(IF(s.status = 'cliente_externo', 1, 0)) AS count_cliente_externo
FROM cursos_modulos AS c
LEFT JOIN subscriptions AS s ON s.curso_id = c.id
LEFT JOIN users AS u ON u.userID = s.user_id WHERE 1";
            if (!empty($id)) { $query .= " AND c.id = '$id'"; }
            if (!empty($ciudad)) { $query .= " AND c.ciudad = '$ciudad'"; }
            if (!empty($tipo)) { $query .= " AND c.tipo = '$tipo'"; }
            if (!empty($titulo))  { $query .=" AND c.titulo = '$titulo'"; }
            if (!empty($status))  { $query .= " AND c.status = '$status'"; } 
$query .= " GROUP BY c.id";

关于php - SQLSTATE[42000] 聚合函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32287304/

相关文章:

php - 如何在每次迭代中以从上到下的方式旋转数组元素

php - Cakephp 3 : Set virtual property with condition on belongsToMany association

mysql - 数据库结构: Best Way To Set up Table for a Calendar Work Roster/Schedule (mySQL)?

php - 使用php循环以表格格式显示来自mysql数据库的数据

mysql - 在 Rails3、 Octopus 复制 gem 查询错误期间丢失与 MySQL 服务器的连接

MySQL - "view"表示特定单元格

PHP + MySQL 多数据库网站到带前缀的单数据库网站。

php - MYSQL/PHP - 如何制作 "Sort By"下拉列表以对从 mysql 提取的结果进行排序

MySQL 将 id 的计数平均值添加到带有 id 的现有选择中

sql - SQL Server 中的 if-then