thara Posted July 22, 2012 Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/ Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 does that query really run without syntax error? Link to comment https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363441 Share on other sites More sharing options...
thara Posted July 22, 2012 Author Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363443 Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 Thara, I don't really have the time to wait for you to post the actual query that is giving you the problem with "DISTINCT" Link to comment https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363444 Share on other sites More sharing options...
thara Posted July 22, 2012 Author Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363451 Share on other sites More sharing options...
Pikachu2000 Posted July 22, 2012 Share Posted July 22, 2012 Post the actual query by cutting and pasting it. Not a query that is "like" the one you're having problems with. Link to comment https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363463 Share on other sites More sharing options...
thara Posted July 22, 2012 Author Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363479 Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363541 Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363551 Share on other sites More sharing options...
fenway Posted July 22, 2012 Share Posted July 22, 2012 Remember, DISTINCT is a modifier to select, not a function. Link to comment https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363553 Share on other sites More sharing options...
thara Posted July 23, 2012 Author Share Posted July 23, 2012 Thank you Barand.. My problem is solved with your help... So if you have a time help me to this post http://forums.phpfreaks.com/index.php?topic=362819.0 Thank you... Link to comment https://forums.phpfreaks.com/topic/266072-distinct-key-word-is-not-working/#findComment-1363600 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.