Jump to content

DISTINCT key word is not working


thara

Recommended Posts

Hi everyone..

 

In this query, DISTINCT key word is not working. display duplicate subjects from subject table.. can anybody tell me where I have made mistake?

 

SELECT DISTINCT tutor_category_subject.subject_id, subject.subjects SEPARATOR ', ') AS subjects  
FROM tutor_category_subject
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = 3;

Link to comment
Share on other sites

Sorry..

 

I have made a mistake when I type...

 

This is my query

 

SELECT tutor_option.option_id, GROUP_CONCAT(options.option_name SEPARATOR ', ') AS option_name  
FROM tutor_option
INNER JOIN options ON tutor_option.option_id = options.option_id
WHERE tutor_option.tutor_id = 3;

Link to comment
Share on other sites

OK.. Barand

 

When you free tell me what is the problem in "DISTINCT"

 

I tried like this with my query

 

SELECT DISTINCT tutor_category_subject.subject_id, GROUP_CONCAT(subject.subjects SEPARATOR ', ') AS subjects  
FROM tutor_category_subject
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = 3;

 

But there are duplicate row selecting from subject table. I want to avoid it from my query. In this case it would have same subject more time to a particular tutor under different category.

 

When you have time tell me what is the solution for this..

 

Thank you very much.

Link to comment
Share on other sites

This is actual query that im trying...

 

SELECT DISTINCT tutor_category_subject.subject_id, GROUP_CONCAT(subject.subjects SEPARATOR ', ') AS subjects  
FROM tutor_category_subject
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = $teacherId";

Link to comment
Share on other sites

OK, I tried setting up some data and running your query. The same result was also obtained without the DISTINCT

tutor_category_subject

+----------+------------+

| tutor_id | subject_id |

+----------+------------+

|        1 |          1 |

|        1 |          2 |

|        1 |          3 |

|        2 |          3 |

|        2 |          4 |

|        3 |          1 |

|        3 |          5 |

+----------+------------+

 

subject

+------------+-----------+

| subject_id | subject  |

+------------+-----------+

|          1 | Maths    |

|          2 | Physics  |

|          3 | Chemistry |

|          4 | Biology  |

|          5 | French    |

|          6 | History  |

+------------+-----------+

 

SELECT DISTINCT tutor_category_subject.subject_id, GROUP_CONCAT(subject.subject SEPARATOR ', ') AS subjects 

FROM tutor_category_subject

INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id

WHERE tutor_category_subject.tutor_id = 1;

 

results

+------------+---------------------------+

| subject_id | subjects                  |

+------------+---------------------------+

|          1 | Maths, Physics, Chemistry |

+------------+---------------------------+

 

No duplicate rows and subjects concatenated as expected. To me though, it would make more sense with that GROUP_CONCAT() to be selecting tutor_id instead of subject_id

Link to comment
Share on other sites

Is this the scenario you are describing where a subject appears twice with different catgories?

 

tutor_category_subject

+----------+------------+--------+

| tutor_id | subject_id | cat_id |

+----------+------------+--------+

|        1 |          1 |      0 |

|        1 |          2 |      0 |

|        1 |          3 |      0 |  same subject

|        1 |          3 |      1 |  different category

+----------+------------+--------+

 

SELECT tutor_category_subject.subject_id, GROUP_CONCAT(subject.subject SEPARATOR ', ') AS subjects 

FROM tutor_category_subject

INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id

WHERE tutor_category_subject.tutor_id = 1;

 

results in Chemistry listed twice

+------------+-------------------------------------+

| subject_id | subjects                            |

+------------+-------------------------------------+

|          1 | Maths, Physics, Chemistry, Chemistry|

+------------+-------------------------------------+

 

If so, you can avoid this by putting the DISTINCT inside the GROUP_CONCAT

 

SELECT tutor_category_subject.subject_id, 
    GROUP_CONCAT(DISTINCT subject.subject SEPARATOR ', ') AS subjects  
FROM tutor_category_subject
INNER JOIN subject ON tutor_category_subject.subject_id = subject.subject_id
WHERE tutor_category_subject.tutor_id = 1;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.