您好我的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);
我得到的错误消息如下:
致命错误:未捕获的异常' PDOException'与消息 ' SQLSTATE [42000]:语法错误或访问冲突:1064您有 SQL语法错误;查看与您的手册相对应的手册 MySQL服务器版本,用于在' WHERE 1 AND附近使用正确的语法 c.id =' 1'限制0,30和#39;在第6行'在 E:\ xampp \ htdocs \ admin \ class \ pagination.php:376堆栈跟踪:#0 E:\ XAMPP \ htdocs中\ ADMIN \类\ pagination.php(376): PDOStatement-> execute()#1 E:\ XAMPP \ htdocs中\ ADMIN \类\ pagination.php(202): 分页 - > excecute_query()#2 E:\ XAMPP \ htdocs中\ ADMIN \类\ 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} 在E:\ xampp \ htdocs \ admin \ class \ pagination.php中抛出 376
答案 0 :(得分:1)
where 1
为你做了什么?试着杀了那个。
以下内容不会引发1064错误:
create table cursos_modulos
( id int not null
);
create table subscriptions
( curso_id int not null,
user_id int not null,
status varchar(100) not null
);
create table users
( userID int not null
);
SELECT c.id,
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
答案 1 :(得分:1)
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";