只有当我们尝试导出到csv时,mysql查询才会失败

时间:2015-07-09 17:18:25

标签: mysql csv

背景资料:

我正在尝试将各种表中的数据转储到csv文件中。 当我自己运行实际查询(没有写入csv文件的命令)时,我得到的结果集没有问题。

问题:

当我修改查询以写入csv文件时,我收到错误:

  

ERROR 1356(HY000):查看'racktables.RackObject'引用无效   表格或列或功能或定义者/调查者缺乏   使用它们的权利

代码

以下是执行选择的代码:

SELECT RackObject.id, RackObject.name, RackObject.label, RackObject.asset_no, RackObject.objtype_id, 
   (SELECT min(rack_id) FROM RackSpace WHERE object_id = RackObject.id) as rack_id, 
   (SELECT parent_entity_id AS rack_id FROM EntityLink WHERE child_entity_type='object' AND child_entity_id = RackObject.id AND parent_entity_type = 'rack' ORDER BY rack_id ASC LIMIT 1) as rack_id_2, 
   (SELECT parent_entity_id FROM EntityLink WHERE child_entity_type='object' AND child_entity_id = RackObject.id AND parent_entity_type = 'object' ORDER BY parent_entity_id ASC LIMIT 1) as container_id, 
   (SELECT name FROM RackObject WHERE id = container_id) as container_name, 
   (SELECT objtype_id FROM RackObject WHERE id = container_id) as container_objtype_id, RackObject.has_problems, RackObject.comment, 
   (SELECT COUNT(*) FROM Port WHERE object_id = RackObject.id) as nports,    
   (SELECT domain_id FROM VLANSwitch WHERE object_id = id LIMIT 1) as 8021q_domain_id, 
   (SELECT template_id FROM VLANSwitch WHERE object_id = id LIMIT 1) as 8021q_template_id 
 FROM RackObject 
 ORDER BY RackObject.name;

以下是尝试导出到csv时查询的样子:

  mysql> SELECT RackObject.id, RackObject.name, RackObject.label, RackObject.asset_no, RackObject.objtype_id, 
 (SELECT min(rack_id) FROM RackSpace WHERE object_id = RackObject.id) as rack_id, 
 (SELECT parent_entity_id AS rack_id FROM EntityLink WHERE child_entity_type='object' AND child_entity_id = RackObject.id AND parent_entity_type = 'rack' ORDER BY rack_id ASC LIMIT 1) as rack_id_2, 
 (SELECT parent_entity_id FROM EntityLink WHERE child_entity_type='object' AND child_entity_id = RackObject.id AND parent_entity_type = 'object' ORDER BY parent_entity_id ASC LIMIT 1) as container_id, 
 (SELECT name FROM RackObject WHERE id = container_id) as container_name,  
 (SELECT objtype_id FROM RackObject WHERE id = container_id) as container_objtype_id, RackObject.has_problems, RackObject.comment, 
 (SELECT COUNT(*) FROM Port WHERE object_id = RackObject.id) as nports, 
 (SELECT domain_id FROM VLANSwitch WHERE object_id = id LIMIT 1) as 8021q_domain_id, 
 (SELECT template_id FROM VLANSwitch WHERE object_id = id LIMIT 1) as 8021q_template_id 
 INTO OUTFILE '/var/log/objects.csv' 
 FIELDS ENCLOSED BY '"' 
 TERMINATED BY ';' 
 ESCAPED BY '"' 
 LINES TERMINATED BY '\r\n' 
 FROM RackObject 
 ORDER BY RackObject.name;
ERROR 1356 (HY000): View 'racktables.RackObject' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
mysql> 

到目前为止我尝试了什么:

我已经用Google搜索了错误消息,但到目前为止,我还没有发现任何与我的问题相关的内容。

我目前正在使用csv转储选项...逐个删除,看看它是否是我正在使用的特定指令导致问题。

最后,我打算逐个删除,各个子选择查看它是否是一个表级别的东西......但这对我来说似乎很奇怪,因为我可以运行查询来筛选没有问题。

我也尝试过这个查询......这很好用:

mysql> SELECT * INTO OUTFILE '/var/log/objects.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n' FROM Object;
Query OK, 20079 rows affected (0.08 sec)

mysql> 

任何建议将不胜感激。 谢谢。

编辑1

这是racktables.RackObject的样子:

mysql> desc racktables.RackObject;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| id           | int(10) unsigned | NO   |     | 0       |       |
| name         | char(255)        | YES  |     | NULL    |       |
| label        | char(255)        | YES  |     | NULL    |       |
| objtype_id   | int(10) unsigned | NO   |     | 1       |       |
| asset_no     | char(64)         | YES  |     | NULL    |       |
| has_problems | enum('yes','no') | NO   |     | no      |       |
| comment      | text             | YES  |     | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> 

1 个答案:

答案 0 :(得分:1)

运行简化的scenarion后:

SELECT *
INTO OUTFILE '/var/log/objects.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
FROM RackObject
ORDER BY RackObject.name; 

事实证明,在MySQL中无法从视图中将数据导出到outfile。必须将查询重写为没有视图的版本。