SELECT desks1.desk_id,desk_code,parent_desk,type_desk,import_open,IFNULL(desks_import_open,0) desks_import_open FROM
(SELECT desk_id,desk_code,parent_desk,type_desk,import_open FROM desks_data_base WHERE user_id = {user_id} and parent_desk = {parent_desk}) desks1
LEFT JOIN  
(SELECT desk_id, count(*) desks_import_open
FROM
(SELECT desks1.desk_id, desks2.d FROM (SELECT * FROM desks WHERE user_id = {user_id} and parent_desk = {parent_desk}) desks1,
(SELECT desk_id d,tree FROM desks WHERE user_id = {user_id} AND import_open) desks2
WHERE desks2.tree LIKE CONCAT('%',desks1.desk_id,'%') AND desks2.d <> desks1.desk_id) desks_with_subs
GROUP BY desk_id) desks_import_open ON desks1.desk_id = desks_import_open.desk_id

