在我的查询中按错误分组

时间:2012-07-11 16:49:31

标签: mysql sql

我想知道是否有人可以对下面的错误有所了解。 sql在本地工作正常,但我远程得到以下错误。

SQL查询:

$inQ = "
    SELECT category.category_name, GROUP_CONCAT(subject.subjects SEPARATOR ', ') AS subjects
    FROM category 
    INNER JOIN tutor_category_subject ON tutor_category_subject.category_id = category.category_id
    INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
    WHERE tutor_category_subject.tutor_id = $tutorId AND tutor_category_subject.category_id = $categoryId
"; 
MySQL说:

  

#1140 - 如果没有GROUP BY子句,混合GROUP列(MIN(),MAX(),COUNT(),...)没有GROUP列是非法的

2 个答案:

答案 0 :(得分:0)

就像错误一样:没有GROUP BY子句,您不能混合组列和非组列。尝试添加一个:

SELECT category.category_name, GROUP_CONCAT(subject.subjects SEPARATOR ', ') AS subjects
FROM category 
INNER JOIN tutor_category_subject ON tutor_category_subject.category_id = category.category_id
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = $tutorId AND tutor_category_subject.category_id = $categoryId
GROUP BY category.category_name

答案 1 :(得分:-1)

我相信你想要这个:

SELECT category.category_name, GROUP_CONCAT(subject.subjects SEPARATOR ', ') AS subjects
FROM category 
  INNER JOIN tutor_category_subject ON tutor_category_subject.category_id = category.category_id
  INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = $tutorId AND tutor_category_subject.category_id = $categoryId
GROUP BY category.category_name, subject.subjects

您需要添加GROUP BY子句才能让您的选择能够使用GROUP_CONCAT