CREATE VIEW desks_data_import AS
SELECT
    desks_data_base.desk_id AS desk_id,
    desks_data_base.user_id AS user_id,
    desks_data_base.parent_desk AS parent_desk,
    desks_data_base.desk_code AS desk_code,
    desks_data_base.import_open AS import_open,
    desks_data_base.type_desk AS type_desk,
    ifnull(desks_DesksImport_open.countDesksImport_open, 0) AS DesksImport_open,
    ifnull(desks_CardsImport_open.countCardsImport_open, 0) AS CardsImport_open
FROM
    desks_data_base
    LEFT JOIN (
        -- desks_Import_open
        SELECT
            desks.parent_desk AS desk_id,
            COUNT(desks.desk_id) AS countDesksImport_open
        FROM
            desks
        where
            import_open
        GROUP BY
            desks.parent_desk
    ) desks_DesksImport_open ON desks_data_base.desk_id = desks_DesksImport_open.desk_id
    LEFT JOIN (
        -- Cardsimport_open
        SELECT
            desks.desk_id AS desk_id,
            COUNT(cardsImport_open.card_id) AS countCardsImport_open
        FROM
            desks
            JOIN (
                SELECT
                    card_id,
                    tree,
                    user_id
                FROM
                    cards_data_base
                WHERE
                    import_open
            ) cardsImport_open ON desks.user_id = cardsImport_open.user_id
        WHERE
            cardsImport_open.tree LIKE CONCAT('%', desks.desk_id, '%')
        GROUP BY
            desks.desk_id
    ) desks_CardsImport_open ON desks_data_base.desk_id = desks_CardsImport_open.desk_id