Da underforespørgsler ikke er tilladt i visninger, bliver du nødt til at simulere dem ved at oprette flere visninger.
For eksempel vil denne forespørgsel løse dit problem, hvis den udføres direkte:
SELECT
TotalCircles + TotalSquares AS TotalShapes
FROM
(SELECT
BlueCirles + RedCircles AS TotalCircles,
BlueSquares + RedSquares AS TotalSquares
FROM
(SELECT
2 AS BlueCirles,
3 AS RedCircles,
4 AS BlueSquares,
5 AS RedSquares
) AS shapes
) as totals;
Ifølge MySQL-dokumentationen visninger har begrænsningen til ikke at kunne indeholde underforespørgsler i FROM-klausulen. For at omgå denne begrænsning og omdanne denne forespørgsel til en visning skal du dele den op i 3 visninger (en for hver underforespørgsel), hvor den sidste giver den ønskede kombination af felter:
CREATE VIEW shapes AS
SELECT
2 AS BlueCirles,
3 AS RedCircles,
4 AS BlueSquares,
5 AS RedSquares;
CREATE VIEW totals AS
SELECT
BlueCirles + RedCircles AS TotalCircles,
BlueSquares + RedSquares AS TotalSquares
FROM
shapes;
CREATE VIEW result AS
SELECT
TotalCircles + TotalSquares AS TotalShapes
FROM
totals;
SELECT * FROM result;