每个人,这个mysql查询在localhost上正常工作
$query3 = "SELECT tutor_category_subject.subject_id, GROUP_CONCAT( DISTINCT subject.subjects SEPARATOR ', ') AS teaching_subjects
FROM tutor_category_subject
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = $teacherId";
但是在将其上传到实时服务器时,它无法正常工作,我收到此错误消息。
GROUP GROUP(MIN(),MAX(),COUNT(),...)的混合没有GROUP 如果没有GROUP BY子句
,则列是非法的
这是我没有GROUP BY子句的查询输出。
+------------+-------------------------------+
| subject_id | teaching_subjects |
+------------+-------------------------------+
| 8 | Art & Craft |
| 10 | Buddhism |
| 12 | Catholicism |
| 14 | Christianity |
| 16 | Dancing |
| 34 | Accounting |
| 37 | Business Studies |
| 39 | Conbined Mathematics |
| 42 | General Infomation Technology |
+------------+-------------------------------+
有人能告诉我问题可能是什么吗?
谢谢
描述表格
mysql> describe tutor_category_subject;
+-------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------+------+-----+---------+----------------+
| tcs_id | int(4) unsigned | NO | PRI | NULL | auto_increment |
| tutor_id | int(4) unsigned | NO | | NULL | |
| category_id | int(2) unsigned | NO | | NULL | |
| subject_id | int(4) unsigned | NO | | NULL | |
+-------------+-----------------+------+-----+---------+----------------+
mysql> describe subject;
+------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-----------------+------+-----+---------+----------------+
| subject_id | int(2) unsigned | NO | PRI | NULL | auto_increment |
| subjects | varchar(60) | NO | MUL | NULL | |
+------------+-----------------+------+-----+---------+----------------+
我需要选择特定导师的科目。一个导师可以有更多的科目。这就是我在查询中使用'GROUP_CONTACT()'的原因。没有group by子句查询在本地主机中工作。但不是在线。
答案 0 :(得分:1)
错误消息告诉您发生这种情况的原因:您需要GROUP BY
子句。
如果不知道查询应该做什么以及您的数据是什么,很难说您想要分组到底是什么。
我不确定我是否正确理解了你想要的东西,试试这对你是否正确:
SELECT s.subject_id, GROUP_CONCAT(DISTINCT s.subjects)
FROM tutor_category_subject AS tcs
INNER JOIN subject as s
ON tcs.subject_id = s.subject_id
GROUP BY tcs.category_id
WHERE tcs.tutor_id = $teacherId
注意$teacherId
如您所见,我已完全删除了GROUP
内容。如果您的查询产生结果,您希望将多个行合并为一个,那么就需要GROUP_BY my_column
:合并那些在my_column
列中具有相同值的行。 Aggregate Functions用于控制其他列的合并精确程度。