简体   繁体   English

如何查询具有相同表架构的多个SQL表?

[英]How to query multiple SQL tables with the same table schema?

I have below list of tables which is created based on weekly archive. 我有下面的表格列表,这些表格是根据每周存档创建的。

dbtable
dbtable_01_04_2017
dbtable_01_07_2017
dbtable_02_09_2017
dbtable_03_06_2017
dbtable_05_08_2017
dbtable_06_05_2017
dbtable_08_04_2017
dbtable_08_07_2017
dbtable_09_09_2017
dbtable_10_06_2017
dbtable_12_08_2017
dbtable_13_05_2017
dbtable_15_04_2017
dbtable_15_07_2017
dbtable_17_06_2017
dbtable_19_08_2017
dbtable_20_05_2017
dbtable_22_04_2017
dbtable_22_07_2017
dbtable_24_06_2017
dbtable_26_08_2017
dbtable_27_05_2017
dbtable_29_04_2017
dbtable_29_07_2017

All of them has the exact same column structure: 它们都具有完全相同的列结构:

TIME_STAMP, USERNAME, INFO, CAUSE TIME_STAMP,USERNAME,INFO,原因

I need to query all of them at once using the same query, of course only by changing the table name in below example query. 我需要使用同一查询一次查询所有这些查询,当然,只需更改以下示例查询中的表名称即可。

 SELECT * FROM dbtable WHERE USERNAME="XXXX";

I googled a bit hoping that there is a way to wildcard the name in the query but I learned that SQL doesn't support that. 我在Google上搜索了一下,希望有一种在查询中使用通配符的名称,但是我知道SQL不支持该名称。

Basically, I am looking for some kind of for loop to iterate the query to multiple tables. 基本上,我正在寻找某种for循环来将查询迭代到多个表。

Please note that new tables will be added each week based on our archive automation. 请注意,根据我们的存档自动化,每周都会添加新表格。

This is too long for a comment. 这个评论太长了。

The problem is your data structure. 问题是您的数据结构。 Multiple tables with the same format is generally a bad idea. 具有相同格式的多个表通常不是一个好主意。 You should have a single table, with a column for the date. 您应该有一个表,其中包含日期列。

Oh, wait, you are concerned about performance, because each of the existing tables is big. 哦,等等,您担心性能,因为每个现有表都很大。 That is why (most) databases support partitioning. 这就是为什么(大多数)数据库支持分区。 You can read about it in the documentation . 您可以在文档中阅读有关内容

If you don't go down that route, one possibility is to create a view with all the tables -- and then when you add a table alter the view to include the latest table. 如果您不遵循这种方法,则一种可能性是创建一个包含所有表的视图,然后在添加表时更改视图以包括最新表。 Of course, queries will probably end up reading all the tables, so they will not be very efficient. 当然,查询可能最终将读取所有表,因此它们将不会非常有效。

The right solution is to use a single table with multiple partitions. 正确的解决方案是使用具有多个分区的单个表。

您可以编写一个存储过程,查询存在哪些表并在这些表上执行SELECT

UNION ALL is the final solution. UNION ALL是最终解决方案。

SELECT * FROM dbtable WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_01_04_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_01_07_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_02_09_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_03_06_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_05_08_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_06_05_2017 WHERE `USERNAME` LIKE '$variable' UNION ALL 
SELECT * FROM dbtable_08_04_2017 WHERE `USERNAME` LIKE '$variable'

I was able to achieve what I need by using above SQL query. 通过使用上述SQL查询,我可以实现所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询多个表的Information_schema,在查询结果中包含表 - Query Information_schema for multiple tables, include table in query result 如何将多个表(具有相同的列名称)复制到新表 <SQL> 。? - How to copy multiple tables (with same columns names) to a new table <SQL>.? SQL-如何通过链接./关联表查询多个表 - SQL - How to query multiple tables via a link ./ associative table SQL 如何像一张连续的表一样查询多张表? - SQL how to query multiple tables as if they are one continuous table? MySQL为同一架构类型创建多个表的单个表名 - Mysql create a single table name for the same schema type multiple tables 执行查询时,创建多个具有相同初始值但表名称不同的SQL表 - Creating multiple SQL tables that have the same initial values but different table names when the query executes 如何优化来自同一表的多个SELECT的SQL查询? - How to optimize SQL query with multiple SELECTs from the same table? SQL:如何减少查询同一张表的多次时间 - SQL : How to reduce multiple time querying the same table ..Query Optimization 多次引用同一个表的 SQL 递归查询 - SQL recursive query with multiple references to same table SQL查询一列上的多个条件(在同一个表上) - SQL Query Multiple conditions on a column (On same Table)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM