由于视图中不允许子查询,因此您需要通过创建多个视图来模拟它们。
例如,如果直接执行此查询将解决您的问题:
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;
根据MySQL 文档,视图限制不能在 FROM 子句中包含子查询。要解决此限制并将此查询转换为视图,请将其分解为 3 个视图(每个子查询一个),最后一个给出所需的字段组合:
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;