0

我想知道是否有人可以阐明以下错误。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 列的 GROUP 列 (MIN(),MAX(),COUNT(),...) 是非法的

4

2 回答 2

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
于 2012-07-11T16:52:36.693 回答
-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.

于 2012-07-11T16:54:42.033 回答