CREATE VIEW users_data AS
SELECT
    users.user_id AS user_id,
    users.pass AS pass,
    users.Phone AS Phone,
    users.regTzintukim AS regTzintukim,
    users.settings AS settings,
    users.import_id AS import_id,
    ifnull(users_cards.countCards, 0) AS cards,
    ifnull(users_desks.countDesks, 0) AS desks,
    ifnull(users_CardsImport_open.countCardsImport_open, 0) AS CardsImport_open,
    ifnull(users_DesksImport_open.countDesksImport_open, 0) AS DesksImport_open,
    ifnull(users_Repetition.countRepetition, 0) AS Repetition,
    ifnull(users_Repetition.countNews, 0) AS News,
    ifnull(users_Repetition.countimports, 0) AS imports,
    ifnull(users_Repetition.counttoDay, 0) AS toDay,
    ifnull(users_Repetition.countOlds, 0) AS Olds,
    ifnull(users_Tzintukim.Tzintukim, 0) AS Tzintukim
FROM
    users
    LEFT JOIN (
        -- cards
        SELECT
            users.user_id AS user_id,
            COUNT(cards_data_base.card_id) AS countCards
        FROM
            users
            JOIN cards_data_base ON cards_data_base.user_id = users.user_id
        GROUP BY
            users.user_id
    ) users_cards ON users.user_id = users_cards.user_id
    LEFT JOIN (
        -- desks
        SELECT
            users.user_id AS user_id,
            COUNT(desks.desk_id) AS countDesks
        FROM
            users
            JOIN desks ON users.user_id = desks.user_id
        GROUP BY
            users.user_id
    ) users_desks ON users.user_id = users_desks.user_id
    LEFT JOIN (
        -- Cardsimport_open
        SELECT
            users.user_id AS user_id,
            COUNT(CardsImport_open.card_id) AS countCardsImport_open
        FROM
            users
            JOIN (
                SELECT
                    card_id,
                    user_id
                FROM
                    cards_data_base
                WHERE
                    import_open
            ) CardsImport_open ON CardsImport_open.user_id = users.user_id
        GROUP BY
            users.user_id
    ) users_CardsImport_open ON users.user_id = users_CardsImport_open.user_id
    LEFT JOIN (
        -- DesksImport_open
        SELECT
            users.user_id AS user_id,
            COUNT(DesksImport_open.desk_id) AS countDesksImport_open
        FROM
            users
            JOIN (
                SELECT
                    desk_id,
                    user_id
                FROM
                    desks
                where
                    import_open
            ) DesksImport_open ON users.user_id = DesksImport_open.user_id
        GROUP BY
            users.user_id
    ) users_DesksImport_open ON users.user_id = users_DesksImport_open.user_id
    LEFT JOIN (
        -- Repetition
        SELECT
            users.user_id AS user_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
            users
            JOIN cards_data_base ON users.user_id = cards_data_base.user_id
        GROUP BY
            users.user_id
    ) users_Repetition ON users.user_id = users_Repetition.user_id
    LEFT JOIN (
        -- Tzintukim
        SELECT
            Tzintukim.user_id AS user_id,
            COUNT(Tzintukim.tzintuk_id) AS Tzintukim
        FROM
            Tzintukim
        GROUP BY
            Tzintukim.user_id
    ) users_Tzintukim ON users.user_id = users_Tzintukim.user_id