更新时间:2022-12-09 16:37:22
由于视图中不允许使用子查询,因此您需要通过创建多个视图来模拟它们.
Since subqueries are not allowed in views, you will need to simulate them by creating multiple views.
例如,如果直接执行此查询,将解决您的问题:
For example, this query will solve your issue if directly executed:
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个视图(每个子查询一个),最后一个给出所需的字段组合:
According to the MySQL documentation views have the restriction on not being able to contain subqueries in the FROM clause. To work around this limitation and turn this query into a view, break it up into 3 views (one for each subquery) with the last giving the desired combination of fields:
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;