轻松“从视图创建表” mysql 中的语法?
我想创建一个表来缓存视图的结果。有没有一种简单的方法可以根据视图的定义自动定义表,或者我必须从 show create table view
将其拼凑在一起?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还发现,在 mysqldump 输出中,有一些语句将视图创建为表,就在它定义视图之前。我可以解析它们并将它们作为查询运行。
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.
您可以从视图中执行
CREATE TABLE SELECT
来构建它。这应该将视图的结构复制为包含视图的所有行的新表。这是此语句的 MySQL 语法参考。请注意,您需要非常明确地选择列。不建议从源视图执行
SELECT *
。还要确保任何计算列或聚合列都有别名,例如 COUNT(*)、MAX(*)、(col1 + col2) 等。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 reference for this statement.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 likeCOUNT(*), MAX(*), (col1 + col2)
, etc.