Solved

MySQL auto fill table with dates

Posted on 2009-07-04
6
585 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
ID: 24780290
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
ID: 24794063
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
ID: 24795419
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:mechanism
ID: 24795443
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
ID: 24795476
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
ID: 31599794
Thank you, it worked, and I also made it in a stored procedure.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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