This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
6年前关闭。
基本上我有一个每天都有一个表的数据库。我需要查询过去30天的数据,这意味着我需要在30个表中执行select语句。
这是在php中,表的名称是这样的;
table_01_03_13
table_02_03_13
table_03_03_13
table_04_03_13
etc...
我的问题是,考虑到这些表是日期,查询这些表的最佳方法是什么?我是否可以循环过去30天来创建查询字符串?这会给我一个字符串,比如:
SELECT * FROM table_01_03_13, table_02_03_13, table_03_03_13, table_04_03_13....

或者有一个SQL函数可以更快更容易地完成这项工作吗?
每天有一个表的唯一原因,是因为每天有40000条记录添加到数据库中。

最佳答案

为什么不是这样的结构:

create table yourtable (
   id int,
   date_of_event timestamp
   ...
);

然后你就可以
SELECT * FROM yourtable
WHERE date_of_event = '2013-04-10'

甚至在特定的日子:
SELECT * FROM yourtable
WHERE YEAR(date_of_event) < 2013
   AND DAY(date_of_event) = 13

// fetch all the events that occured on 'day 13' or any month in the previous years.

09-30 14:22
查看更多