是否可以在MySQL VIEW中使用先验计算?我已经做了很多工作并在线搜索(包括在SO上搜索),但是我空白了。
如果可能,我必须使用了错误的语法类型。例如,如果我有这样的东西(简化示例),它将给我一个错误,它不知道在哪里可以找到factory_1_revenue,等等。
CREATE VIEW [myview] AS
SELECT
(t1.qty * t1.price) AS factory_1_revenue,
(t2.qty * t2.price) AS factory_2_revenue,
(THIS_VIEW.factory_1_revenue + THIS_VIEW.factory_2_revenue) AS factories_revenue
FROM table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id;
我可以在其之上创建另一个视图,但是我宁愿保持简单并尽可能避免嵌套视图。
以下是更多详细信息:
+-------------------------------+
| table1 |
+-------------------------------+
| id | product_id | qty | price |
+----+------------+-----+-------+
| 1 | 1 | 50 | 3.00 |
+----+------------+-----+-------+
| 2 | 2 | 65 | 2.00 |
+----+------------+-----+-------+
| 3 | 3 | 70 | 2.50 |
+----+------------+-----+-------+
+-------------------------------+
| table2 |
+-------------------------------+
| id | product_id | qty | price |
+----+------------+-----+-------+
| 1 | 1 | 40 | 3.00 |
+----+------------+-----+-------+
| 2 | 2 | 20 | 2.00 |
+----+------------+-----+-------+
| 3 | 3 | 30 | 2.50 |
+----+------------+-----+-------+
本质上,给定在VIEW的SELECT语句中创建的AS别名的情况下,如何引用计算值:
( factory_1_revenue + factory_2_revenue )AS factory_revenue
没有出现错误?
(我的实际视图,数据库等要复杂得多,我试图简化/最小化它来显示我的问题。)