Jump to content

How Can I Fix This Select Query....


thara

Recommended Posts

This is my query..

 

SELECT tcs.tutor_id AS tid, tcs.category_id AS cid, tcs.subject_id AS sid, GROUP_CONCAT( DISTINCT s.subjects SEPARATOR ', ') AS subjects,
t.tutor_name, t.tutor_code AS tcode, DATE_FORMAT(t.registration_date, '%b %D, %Y') AS date, t.qualification,
GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, timg.image_name AS img
FROM tutor_category_subject as tcs
INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
INNER JOIN tutor_option AS toption ON toption.tutor_id = tcs.tutor_id
INNER JOIN options AS o ON toption.option_id = o.option_id
INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id
WHERE s.subjects LIKE '%business%' AND timg.image_type = 'profile'
GROUP BY tcs.tutor_id;

 

This query working properly.. But there is problem with timg's image_type coloum. It is tutor may have their profile image and some tutors dont have profile image. But I need to select all tutors with this condition even if not their profile images. In this query, if there is no profile image to the tutor that tutor not selecting... so how can I do this..?

 

any comments are greatly appreciated.

Thank you

Edited by thara
Link to comment
Share on other sites

You could use a CASE in the WHERE :

WHERE ((s.subjects LIKE '%business%') AND (CASE WHEN SELECT timg.image_type = 'profile' IS NOT NULL THEN timg.image_type = 'profile' ELSE timg.image_type = 'something else'))

 

 

This isn't that close to being actualy correct, but if you look up the MySQL manual on CASE then it should be enough for you to be able to fix it.

Link to comment
Share on other sites

 

I can't see how that helps select the profile image if it is there, and a different image if it's not.....could you explain the logic here?

 

When you use LEFT JOIN there are not always values present from the left-joined table (or you may as well have INNER JOIN). For that reason it's useless having a condition in the WHERE clause that refers to that table - it needs to go in the JOIN condition so that only those records are matched.

Link to comment
Share on other sites

... But I need to select all tutors with this condition even if not their profile images. ...

 

I did not see anything in the OP's posts about selecting a different image if the profile image is not present. If there are other images that need to be chosen when the "profile" image does not exist, we would need to know the structure and selection criteria in order to make a suggestion.

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.