CREATE TEMPORARY TABLE inputs(remainder INT, modulus INT);

INSERT INTO inputs VALUES (2, 3), (3, 5), (2, 7);

WITH recursive

-- Multiply out the product of moduli
multiplication(idx, product) AS (
    SELECT 1, 1

    UNION ALL

    SELECT
        multiplication.idx+1,
        multiplication.product * inputs.modulus
    FROM
        multiplication,
        inputs
    WHERE
        inputs.rowid = multiplication.idx
),

-- Take the final value from the product table
product(final_value) AS (
    SELECT MAX(product) FROM multiplication
),

-- Calculate the multiplicative inverse from each equation
multiplicative_inverse(id, a, b, x, y) AS (
    SELECT
        inputs.modulus,
        product.final_value / inputs.modulus,
        inputs.modulus,
        0,
        1
    FROM
        inputs,
        product

    UNION ALL

    SELECT
        id,
        b, a%b,
        y - (a/b)*x, x
    FROM
        multiplicative_inverse
    WHERE
        a>0
)
-- Combine residues into final answer
SELECT
    SUM(
        (y% inputs.modulus) * inputs.remainder * (product.final_value / inputs.modulus)
    )% product.final_value
  FROM
    multiplicative_inverse, product, inputs
  WHERE
    a=1 AND multiplicative_inverse.id = inputs.modulus;