mysql 视图示例
基本操作
建立视图
CREATE VIEW view_test(qty,price,total) AS SELECT quantity,price,quantity*price FROM t;
多表视图
CREATE VIEW stu_class(id,name,glass) AS SELECT student.s_id,student.name,stu_info.glass FROM student,stu_info WHERE student.s_id = stu_info.s_id;
查看视图
DESCRIBE 视图名
DESC 视图名
查看视图基本信息
SHOW TABLE STATUS LIKE '视图名';
查看视图的建表信息
SHOW CREATE VIEW 视图名
在VIEW表中查看视图详细信息
在mysql的information_schema数据库下的views表中存储了所有的视图定义,可以用select查看
select * from information_schema.views;
修改视图
REPLACE语句
CREATE OR REPLACE VIEW view_test AS SELECT * FROM t;
ALTER语句
ALTER VIEW view_test SELECT name FROM t;
删除视图
DROP VIEW IF EXISTS stu_glass
建表语句示例
/*获取系统组织结构
*包括用户名称、用户ID、所属店面名称、所属店面ID、创建时间...
*/
CREATE VIEW organizationTableView as select id,storename,regdate from v9_qd_account;
/*获取当天的Cpz安装数据(按用户分组汇总)
*包括用户ID、Cpz汇总值...
*/
CREATE
VIEW TodayCpzTableView as select storeid,storename,count(id)as
total,sum(tui_num)as tui_num from v9_qd_dev where days =
DATE_FORMAT(NOW(),'%Y%m%d') group by storeid;
/*获取当月每天的Cpz安装数据(按天、用户分组汇总)
*包括日期(天)、用户ID、Cpz汇总值、CpzApp汇总值、重复Cpz汇总值...
*/
CREATE
VIEW HistoryCurrentMonthDayView as select storeid,storename,count(id)as
total,sum(tui_num)as tui_num,days from v9_qd_dev where days >
DATE_FORMAT(NOW(),'%Y%m') group by storeid,days;
/*获取每月的Cpz安装数据(按月、用户分组汇总)
*包括日期(天)、用户ID、Cpz汇总值、CpzApp汇总值、重复Cpz汇总值...
*/
CREATE
VIEW HistoryMonthTableView as select storeid,storename,count(id)as
total,sum(tui_num)as tui_num,
DATE_FORMAT(FROM_UNIXTIME(installdate),'%Y%m') as months from v9_qd_dev
group by storeid,months;
/*根据输入的IMEI获取Cpz安装数据()
*包括IMEI、账户ID、店面ID、安装时间、是否重复安装、安装app个数、手机型号
*/
CREATE VIEW QueryCpzDatumByIMEI as select storeid,storename,verify,tui_num,sn,idfa,imei,devms,installdate from v9_qd_dev;
//今天
DATE_FORMAT(NOW(),'%Y%m%d');