doubei2340 2015-03-29 04:49
浏览 13
已采纳

mysql - 根据创建日期选择最后两个表

I need some advise on implementing my database schema properly. I have a cron script whose main functions are to Parse HTML docs => Create a DB tbl => Insert Records

I am used to run this script only once per month but I need to do it more frequently which means my database tables will increase significantly. At the moment my tables follow this format : table_03 where the last two digits represent the current month.

Now I am considering using PHP time() function to replace the current month.
My first question, is this a good way to approach it ?

My second issue is how do you go about creating a dynamic SELECT statement that fetches the last two tables in a Database based on their date ? or could be better to know if there is an MySQL query that does this job instead of relying of the table names ?

e.g

table_29_03 // 29 March
table_26_03 // 26 Mar...
table_25_03
...

My query should return the difference between the last two or three table, but not having consistent dates ( as monthly ) I am not sure how to do it.

At the moment I am doing the following :

$thisMonth = 'table_'.date('m');
$PrevMonth = 'table_'.date('m', strtotime('first day of last month'));

// find new records in this table not available in previous one
$sql = "
  SELECT * FROM `".$thisMonth."` WHERE `".$thisMonth."`.`id`
  NOT IN (
  SELECT `".$PrevMonth."`.`id`
  FROM `".$PrevMonth."`
  WHERE `".$thisMonth."`.`id` = `".$PrevMonth."`.`id`
          ); ";
  • 写回答

1条回答 默认 最新

  • dongwenxiu5200 2015-03-29 05:21
    关注

    My second issue is how do you go about creating a dynamic SELECT statement that fetches the last two tables in a Database based on their date ?

    is this what you want to achieve in MySQL?

    SELECT create_time,table_name 
    FROM INFORMATION_SCHEMA.TABLES 
    ORDER BY create_time DESC LIMIT 2
    

    you can add additional filters like what schema should we extract the data from, etc...

    As for your first question, I am using gmdate as of now to extract the month and year of time.

    $now = time();
    $month =  gmdate("m", $now);
    $year =  gmdate("y", $now);
    

    gmdate: time returned is in Greenwich Mean Time (GMT).

    using time(); to set your month is alright and i can see no problem in that.

    Side Note: If you are running PHP and MySQL on separate machines, there would be some discrepancies in time if the datetime of those two are not synced. if you want an accurate time representation of both. You should be dependent on one machine.

    If you want PHP to handle time logging, you can include a column in in your tables like 'created_at' which indicates the time when it was generated, based on PHP time();

    If you want MySQL to handle it, you can query first the current date select curdate(); then create the table names based on the month you fetched from your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探