mysql 中的简单“从视图创建表”语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9241922/
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
Easy 'create table from view' syntax in mysql?
提问by user151841
I want to create a table that's a cache of results from a view. Is there an easy way to automatically define the table from the view's definition, or will I have to cobble it together from show create table view
?
我想创建一个表,它是视图结果的缓存。有没有一种简单的方法可以从视图的定义中自动定义表,还是我必须将它拼凑在一起show create table view
?
回答by Michael Berkowski
You can do CREATE TABLE SELECT
from the view to build it. That should duplicate the view's structure as a new table containing all the view's rows. Here's the MySQL syntax referencefor this statement.
你可以CREATE TABLE SELECT
从视图来构建它。这应该将视图的结构复制为一个包含所有视图行的新表。这是此语句的MySQL 语法参考。
CREATE TABLE tbl_from_view AS
SELECT
col1,
col2,
col3,
col4,
col5
FROM your_view;
Note that you will want to be very explicit in your column selections. It isn't advisable to do a SELECT *
from the source view. Make sure as well that you have aliases for any calculated or aggregate columns like COUNT(*), MAX(*), (col1 + col2)
, etc.
请注意,您将希望在列选择中非常明确。不建议SELECT *
从源视图执行 a 。还要确保您为任何计算列或聚合列(如COUNT(*), MAX(*), (col1 + col2)
等)设置了别名。
回答by user151841
I also found that in the mysqldump output, there are statements that create the view as a table, just before it defines the view. I can parse those out and run them as queries.
我还发现在 mysqldump 输出中,有语句将视图创建为表,就在它定义视图之前。我可以解析它们并将它们作为查询运行。