MySQL 从 INFORMATION_SCHEMA 检索的一组表中进行 SELECT
嘿大家。我有以下一组表格,这些表格是可变的并且每天都会增加:
data-2010-10-10
data-2010-10-11
data-2010-10-12
data-2010-10-13
等等。所有表都具有相同的结构,我想做的是立即从所有表中选择内容。由于我正在运行 InnoDB,因此无法使用 MERGE 表。无论如何,我使用以下语句从我的 information_schema 中选择表名:
select table_name from `information_schema`.`tables`
where `table_schema` = 'mydb2' and `table_name` like 'data-%'
它返回我想要合并的所有表。我还可以对返回的结果进行 group_concat 以获得以逗号作为分隔符的列表。现在我陷入困境的是运行一个选择查询,该查询实际上会从这些表中检索数据。
任何提示表示赞赏。谢谢! 〜K
Hey all. I've got the following set of tables which is variable and adds up every day:
data-2010-10-10
data-2010-10-11
data-2010-10-12
data-2010-10-13
And so on. All the tables have the same structure and what I'd like to do is select stuff from all tables at once. I'm unable to use a MERGE table since I'm running InnoDB. Anyways, I'm using the following statement to select the table names from my information_schema:
select table_name from `information_schema`.`tables`
where `table_schema` = 'mydb2' and `table_name` like 'data-%'
Which return all the tables I'd like to unite. I can also group_concat the returned results to get the listed with a comma as a separator. Now where I'm stuck is running a select query that would actually retrieve the data from these tables.
Any hints are appreciated. Thanks! ~ K
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用动态 SQL 来构建查询(或多个查询)才能从这些表中获取结果。也就是说,您从 information_schema 查询中以字符串形式获取表,然后将这些字符串插入到进一步的查询中。
涉及对所有表进行单个查询的 UNION 的事情:
但我质疑您每天创建一个单独的表的设计。起初这似乎是一件很方便的事情,但是表的传播失去了控制,并且您最终在执行普通查询时遇到了困难,就像您现在面临的困难一样。这是一个反模式,我称之为元数据Tribbles。
您的数据很可能可以存储在单个表中,并使用日期列来区分不同日期的数据。您可能只需要创建索引来协助对总数据运行所需的查询。
回复您的评论:
您不能在 SQL 查询(包括视图定义)中使表名动态化。您可以将表名称插入字符串中,然后将该字符串准备为 SQL 查询。
您可以使用
CONCAT() 构建字符串
函数,然后使用PREPARE
和EXECUTE
将字符串作为查询运行。但由于 mysql 客户端中没有循环结构,如果您无法使用 Python 或 PHP 或某些此类主机语言编写脚本,则必须编写一个存储过程来执行此操作。You would have to use dynamic SQL to build the query (or queries) to get results from these tables. That is, you get the tables back from your information_schema query as strings, then you interpolate these strings into a further query.
Something involving UNION of individual queries against all the tables:
But I question your design of creating a separate table per day. It seems like a convenient thing to do at first, but the tables propagate out of control, and you end up having difficulty doing ordinary queries, like the difficulty you are facing now. This is an antipattern I call Metadata Tribbles.
It's likely that your data could be stored in a single table, with a date column to let you distinguish data from different days. You probably just need to create indexes to assist the queries you need to run against the total data.
Re your comment:
You can't make table names dynamic in an SQL query (including a view definition). You can interpolate table names into a string, and then prepare the string as an SQL query.
You can build a string with the
CONCAT()
function, and then usePREPARE
andEXECUTE
to run the string as a query. But since there are no loop structures in the mysql client, you'd have to write a stored procedure to do this if you can't write a script in Python or PHP or some such host language.