CREATE VIEW desks_data_repetition AS
SELECT
    desks.desk_id AS desk_id,
    desks.desk_code AS desk_code,
    desks.user_id AS user_id,
    desks.parent_desk AS parent_desk,
    ifnull(desks_Repetition.countRepetition, 0) AS Repetition,
    ifnull(desks_Repetition.countNews, 0) AS News,
    ifnull(desks_Repetition.countimports, 0) AS imports,
    ifnull(desks_Repetition.counttoDay, 0) AS toDay,
    ifnull(desks_Repetition.countOlds, 0) AS Olds
    FROM desks
    LEFT JOIN (
        -- Repetition
        SELECT
            desks.desk_id AS desk_id,
            SUM(cards_data_base.Repetition) AS countRepetition,
            SUM(cards_data_base.News) AS countNews,
            SUM(cards_data_base.imports) AS countimports,
            SUM(cards_data_base.Olds) AS countOlds,
            SUM(cards_data_base.toDay) AS counttoDay
        FROM
            desks
            JOIN cards_data_base ON desks.user_id = cards_data_base.user_id
        WHERE
            cards_data_base.tree LIKE CONCAT('%', desks.desk_id, '%')
        GROUP BY
            desks.desk_id
    ) desks_Repetition ON desks.desk_id = desks_Repetition.desk_id
   