CREATE OR REPLACE VIEW `vizon_web_rankings` AS
SELECT
    bets.id,
    vizon_drawings_id,
    COUNT(*) AS richtige
FROM
    (
    SELECT
        vizon_users_id AS id,
        vizon_drawings_id,
        FIRST AS NUMBER
    FROM
        vizon_bets
    UNION ALL
SELECT
    vizon_users_id,
    vizon_drawings_id,
    SECOND
FROM
    vizon_bets
UNION ALL
SELECT
    vizon_users_id,
    vizon_drawings_id,
    third
FROM
    vizon_bets
UNION ALL
SELECT
    vizon_users_id,
    vizon_drawings_id,
    fourth
FROM
    vizon_bets
UNION ALL
SELECT
    vizon_users_id,
    vizon_drawings_id,
    fifth
FROM
    vizon_bets
UNION ALL
SELECT
    vizon_users_id,
    vizon_drawings_id,
    sixth
FROM
    vizon_bets
) bets,
(
    SELECT
        id,
        FIRST AS NUMBER
    FROM
        vizon_drawings
    UNION ALL
SELECT
    id,
    SECOND
FROM
    vizon_drawings
UNION ALL
SELECT
    id,
    third
FROM
    vizon_drawings
UNION ALL
SELECT
    id,
    fourth
FROM
    vizon_drawings
UNION ALL
SELECT
    id,
    fifth
FROM
    vizon_drawings
UNION ALL
SELECT
    id,
    sixth
FROM
    vizon_drawings
) drawings
WHERE
    bets.vizon_drawings_id = drawings.id AND bets.number = drawings.number
GROUP BY
    bets.id,
    vizon_drawings_id