DELIMITER $$
--
-- Создать функцию "calc_date_range"
--
CREATE FUNCTION calc_date_range(date_start DATE, date_end DATE)
RETURNS text CHARSET latin1
SQL SECURITY INVOKER
BEGIN
DECLARE res_text text;
DECLARE tmp_date date;
IF(date_end < date_start) THEN
RETURN '';
END IF;
SET tmp_date := date_start;
SET res_text :='';
WHILE(tmp_date<=date_end) DO
SET res_text := CONCAT(res_text,',',DATE_FORMAT(tmp_date, '%m-%d-%Y'));
SET tmp_date := DATE_ADD(tmp_date, INTERVAL 1 DAY);
END WHILE;
RETURN SUBSTRING(res_text,2);
END
SELECT c.cdate FROM tasks t
INNER JOIN calendar c ON c.cdate >= DATE(t.date_from) AND c.cdate<=DATE(t.date_to)
WHERE
t.USER_ID = 17
AND t.status = 4
AND NOT c.weekend = 1
GROUP BY c.cdate;