2012-10-31 166 views
0

這是我的查詢..我怎樣才能解決這個選擇查詢

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; 

此查詢工作正常。但有一個與TIMG的IMAGE_TYPE問題明杆。這是輔導員可能有他們的個人資料圖像和一些輔導員沒有配置文件圖像。但是我需要選擇所有具有這種條件的導師,即使他們的個人資料圖片不是。在這個查詢中,如果導師沒有選擇...的導師沒有個人資料圖片,那麼我該怎麼做呢?

任何意見,不勝感激。 謝謝

回答

3

你只需要到WHERE子句中移動過濾器的JOIN子句tutor_images

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 AND timg.image_type = 'profile' 
WHERE s.subjects LIKE '%business%' 
GROUP BY tcs.tutor_id; 
1

你也能做到這一點的WHERE部分:

WHERE s.subjects LIKE '%business%' AND (timg.image_type = 'profile' OR timg.image_type is NULL) 
相關問題