将 MySQL 视图转储为包含数据的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/541322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dump MySQL view as a table with data
提问by barryhunter
Say I have a view in my database, and I want to send a file to someone to create that view's output as a table in their database.
假设我的数据库中有一个视图,我想向某人发送一个文件,以将该视图的输出创建为他们数据库中的一个表。
mysqldump of course only exports the 'create view...' statement (well, okay, it includes the create table, but no data).
mysqldump 当然只导出 'create view...' 语句(好吧,好吧,它包括创建表,但没有数据)。
What I have done is simply duplicate the view as a real table and dump that. But for a big table it's slow and wasteful:
我所做的只是将视图复制为一个真实的表并将其转储。但是对于一张大桌子来说,它既慢又浪费:
create table tmptable select * from myview
Short of creating a script that mimics the behaviour of mysqldump and does this, is there a better way?
除了创建一个模仿 mysqldump 行为的脚本并执行此操作之外,还有更好的方法吗?
回答by Paul Tomblin
One option would be to do a query into a CSV file and import that. To select into a CSV file:
一种选择是对 CSV 文件进行查询并导入。要选择到 CSV 文件中:
From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
来自http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
回答by Jeff Ferland
OK, so based on your CSV failure comment, start with Paul's answer. Make the following change to it:
好的,因此根据您的 CSV 失败评论,从 Paul 的回答开始。对其进行以下更改:
- FIELDS TERMINATED BY ','
+ FIELDS TERMINATED BY ',' ESCAPED BY '\'
When you're done with that, on the import side you'll do a "load data infile" and use the same terminated / enclosed / escaped statements.
完成后,在导入端,您将执行“加载数据 infile”并使用相同的终止/封闭/转义语句。