ugnuku
ugnuku

Reputation: 161

Query work with localhost and not work in live server

Everyone, this mysql query is working properly on 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";

But on uploading it to the live server, it's not working and I get this error message.

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

This is my query output without GROUP BY clause.

 +------------+-------------------------------+
| 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 |
+------------+-------------------------------+

Can someone tell me what the problem might be?

Thank you

describe tables

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    |                |
+------------+-----------------+------+-----+---------+----------------+

I need to select subjects to a particular tutor. One tutor could have more subjects. thats why I used 'GROUP_CONTACT()' in my query. Without group by clause query is working in local host. But not in online.

Upvotes: 0

Views: 1619

Answers (1)

phant0m
phant0m

Reputation: 16905

The error message tells you why that happens: You need a GROUP BY clause.

It's difficult to say what exactly you want to group on without knowing what the query should do and what your data is.

I'm not sure whether I have understood correctly what you want, try whether this does the correct thing for you:

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

Pay attention to SQL injections for $teacherId

As you can see, I have completely removed the GROUPing stuff. If your query yields a result, where you would like to combine multiple rows into one, that is when you need GROUP_BY my_column: merge those, that have an equal value in column my_column. Aggregate Functions are used to control how exactly the other columns are merged.

Upvotes: 1

Related Questions