Solved

MySQL auto fill table with dates

Posted on 2009-07-04
6
582 Views
Last Modified: 2012-05-07
I have a table named "dbDiary" with fields "fDate" and "fDay".
I want to fill "fDate" field with dates starting July 1st, 2009, in format YYYY-MM-DD and for each value to have the "fDay" field populated with the day number (Monday=1, ..., Sunday=7) - up to 2012, December 31st.

Example of dbDiary table:      

fDate           fTable
--------------------------------
20090713      1
20090714      2
20090715      3
20090716      4
20090717      5
20090718      6
20090719      7
20090720      1
20090721      2
20090722      3
20090723      4
20090724      5
20090725      6
20090726      7
20090727      1
20090728      2
etc..

How can I do this?
0
Comment
Question by:mechanism
  • 3
  • 2
6 Comments
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
You could build a simple stored procedure to make this happen, but the better question is why you feel you need to.  What is the purpose behind populating the table in this manner?
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 50 total points
Comment Utility
Insert one record, then insert...select a few times to get the rest of the records:
INSERT INTO dbDiary (fDate) VALUES ('2009-07-01');
 

INSERT INTO dbDiary (fDate) SELECT fDate + INTERVAL (SELECT COUNT(*) FROM dbDiary) DAY FROM dbDiary;

-- Repeat that last insert 10 times, which will populate the table up through 2015
 

DELETE FROM dbDiary WHERE fDate > '2012-12-31';

UPDATE dbDiary SET fDay = DAYOFWEEK(fDate);

Open in new window

0
 

Author Comment

by:mechanism
Comment Utility
Answering to routinet.

Your comment is valid.
I have a number of tables that need to be loaded each Monday, Tuesday, Wednesday etc. On each day (first column) I want to load files in tables ending in '1' (for example, file blabla.txt into table BlaBla_1, on next day, suppose a Tuesday, file blabla.txt into table BlaBla_2, next day on BlaBla_3 etc).
I need to have a file from each day of the week from another system into MySql but on a weekly basis. Again, using the same example, after a week, I need to overwrite table BlaBla_1 with the new blabla.txt file etc.
 
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mechanism
Comment Utility
Answering to snoyes_jw

Please allow me to get back to you, I am now checking this.. Thank you for comment..
0
 
LVL 33

Expert Comment

by:snoyes_jw
Comment Utility
Instead of all those separate tables, why not one table, with a field that indicates the day of the week? Then you can delete the old records and insert the new ones, without all the dynamic queries and such that multiple tables imply.

Using 5.1's partitioning would make it even easier.
0
 

Author Closing Comment

by:mechanism
Comment Utility
Thank you, it worked, and I also made it in a stored procedure.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now