刚刚开始阅读视图教程
我使用了这个链接view tutorial
在本教程中他们说
Subquery cannot be included in the SELECT statement.
但他们举了一个例子,
CREATE VIEW vwProducts AS
SELECT productCode,
productName,
buyPrice
FROM products
WHERE buyPrice > (
SELECT AVG (buyPrice)
FROM products
)
ORDER BY buyPrice DESC
告诉我,是否可能,
在视图中我们是否应该使用子查询,
答案 0 :(得分:7)
SELECT语句不能在FROM子句中包含子查询。
您的示例在WHERE子句中包含子查询。
如果在FROM子句中确实有子查询,则解决方法是使子查询成为自己的视图。
答案 1 :(得分:3)
似乎在5.0.27
中工作,你的版本是什么?
A view can be created from many kinds of SELECT statements. It can refer to base tables or other views. It can use joins, UNION, and subqueries. The SELECT need not even refer to any tables. The following example defines a view that selects two columns from another table, as well as an expression calculated from those columns:
- http://dev.mysql.com/doc/refman/5.0/en/create-view.html
答案 2 :(得分:1)
通常SELECT语句可以有SELECT子查询,但与其他DBMS相反'MySQL的限制是无法从包含子查询的select语句创建视图。如果为子查询创建一个视图,然后使用此视图创建最初想要的视图,则可以轻松克服此限制。
查看MySQL错误报告:http://bugs.mysql.com/bug.php?id=16757