Alternative to using a subquery - can't create view in Mysql

泄露秘密 提交于 2019-12-08 13:21:06

问题


I have created a view that pulls a range of rows based on a calculation; using this script (per this question: How to select Range of rows based on field values - MySQL :

select t.*
from curdataEvents t cross join
     (select max(revs) as maxrev from curdataEvents) x
where t.revs >= x.maxrev - 100000;

This pulls the range of rows I need. In order to get the desired report - I have to create multiple views each creating the next layer of the report. The problem is MySQL won't create a view using a subquery. Any ideas on how to re-write the script above that would yield the same results but allow me to create a view? I have tried multiple variations using a UNION clause, etc. What's tripping me up is this is a join to itself. The examples I've found so far using multiple tables. Any help is greatly appreciated!!!

Thanks


回答1:


You can use a subquery, just not in the FROM clause. Just move it to the WHERE clause;

CREATE VIEW view1 AS 
SELECT t.*
FROM curdataEvents t 
WHERE t.revs >= (SELECT MAX(revs) - 100000 AS maxrev FROM curdataEvents)

An SQLfiddle to test with.



来源:https://stackoverflow.com/questions/34597807/alternative-to-using-a-subquery-cant-create-view-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!