2010-11-11 85 views
0

上午剛開始讀查看教程MySQL視圖支持子查詢

我在本教程中,他們說

Subquery cannot be included in the SELECT statement. 

使用這個鏈接view tutorial

,但他們給了一個例子是,

CREATE VIEW vwProducts AS 
SELECT productCode, 
     productName, 
     buyPrice 
FROM products 
WHERE buyPrice > (
     SELECT AVG (buyPrice) 
     FROM products 
) 
ORDER BY buyPrice DESC 

告訴我,是否有可能,

鑑於我們是否使用子查詢或不,

+2

爲什麼不試試? – 2010-11-11 12:46:28

+0

它的工作try..but在很多地方,他們說在視圖中沒有更多的子查詢 – Bharanikumar 2010-11-11 12:47:54

回答

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

7

SELECT語句不能包含FROM子句中的子查詢。

您的示例在WHERE子句中包含子查詢。

如果您在FROM子句中有子查詢,則解決方法是使子查詢爲自己的視圖。

1

通常,SELECT語句可以具有SELECT子查詢,但與其他DBMS相反'MySQL的限制是不能從包含子查詢的select語句創建視圖。如果爲子查詢創建一個視圖,然後使用此視圖創建最初想要的視圖,則可以輕鬆克服此限制。

看看MySQL的bug報告:http://bugs.mysql.com/bug.php?id=16757