传统方式:
select cat_id,avg(shop_price) as aprice from goods group by cat_id order by aprice desc limit 3;
#创建视图: create view v1 as select cat_id,avg(shop_price) as aprice from goods;#查看视图:Show tables;#查看视图列:Desc 视图名#查看视图创建:Show create view 视图名#删除视图:Drop view 视图名新的方式:利用创建视图
视图的好处:
1. 简化查询。
2. 权限控制。
3. 分表查询.
4. 可维护性好。、
A .简化查询,上面的例子用视图实现的代码:
创建视图表Create view v1 as Select gooods_id,cat_id,goods_name,avg(shop_price) as aprice from esc_goods group by cat_id;#查询视图表Select *from v1 order by aprice limit 3;
改变视图的算法:
Create algorithm = temptable view 视图名 asmysql> create algorithm=temptable view vec as -> select goods_id,goods_name,cat_id,shop_price ->from goods order by cat_id,shop_price desc;