我正在运行一个查询,该查询基于某些视图进行计算,并将结果写入文件。它看起来像这样:
select `v1`.`id` AS `id`,`v2`.`name` AS `name`,
`v2`.`nbr` AS `nbr`,
`v1`.`nbr` AS `total`,
(`v2`.`nbr` / `v1`.`nbr`) AS `percent`
INTO OUTFILE '/home/username/mysqldump/filename.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
from `v2` join `v1`
where `v2`.`id` = `v1`.`id`
这很好用。我基于此查询创建了一个视图,但正如我预期的那样,它不能作为直接替换。这是 SHOW CREATE VIEW 的“创建视图”部分:
CREATE ALGORITHM=UNDEFINED DEFINER=`user`@`localhost`
SQL SECURITY DEFINER
VIEW `view_name` AS
select `v1`.`id` AS `id`,
`v2`.`name` AS `name`,
`v2`.`nbr` AS `nbr`,
`v1`.`nbr` AS `total_pvs`,
(`v2`.`nbr` / `v1`.`nbr`) AS `percent`
from (`v2` join `v1`) where (`v2`.`id` = `v1`.`id`)
这是不起作用的查询:
SELECT * INTO OUTFILE '/home/username/mysqldump/filename.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM view_name;
我收到了这个错误:
ERROR 1356 (HY000): View 'database_name.view_name' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
我以 root 身份运行,所以我认为没有任何权利问题。是什么让这个查询在 MySQL 眼中无效?我正在运行版本 5.5.32-0ubuntu0.12.04.1 (Ubuntu)
编辑:运行SELECT * FROM view_name
按预期返回记录。但是,由于有数千行,并且需要纯格式,我宁愿使用INTO OUTFILE
.