Skip to main content
5 of 5
added 277 characters in body
ypercubeᵀᴹ
  • 98.6k
  • 13
  • 215
  • 305

Your solution will cause performance problems if there will be larger number of records as there will be not index used in this JOIN statement:

LEFT join (SELECT DISTINCT id_task FROM PROJECT_HAS_TASK)b on (a.id_task = b.id_task);

Probably you should try something using the EXISTS statement like this

SELECT `TASK`.`id_task`,  
CASE WHEN EXISTS (SELECT * FROM `PROJECT_HAS_TASK` 
                  WHERE `TASK`.`id_task` = `PROJECT_HAS_TASK`.`id_task`) 
  THEN 1 
  ELSE 0 
END as HasProject 
FROM `TASK`;

or the simplified (for MySQL only):

SELECT t.id_task,  
       EXISTS (SELECT * FROM PROJECT_HAS_TASK AS pt 
               WHERE t.id_task = pt.id_task) 
         AS HasProject 
FROM TASK AS t;