延續上一篇中建立的foodsum檢視表,與子查詢來做比較
--建立foodsum檢視表--
CREATE VIEW foodsum ( food_catalg, count_food)
AS
SELECT food_catalg, count(*)
FROM food
GROUP BY food_catalg;
--確認檢視表資料內容—
SELECT food_catalg , count_food
From foodsum;
SELECT food_catalg , count_food
FROM(SELECT food_catalg, count(*) AS count_food
FROM food
GROUP BY food_catalg) AS foodsum;
--AS foodsum為賦予此子查詢的名稱,也是用過即捨棄的名稱--
這兩種方法執行結果是一樣的
一樣使用food資料表
SELECT AVG(sell_price)From food
--執行結果為26--
SELECT food_id , food_name , sell_price
FROM food
WHERE sell_price < (SELECT AVG(sell_price)From food);
/--------此部分會變換成26-------/
執行結果 : 小於26的sell_price