Solved

Get data from MySql table and store into array then write to antoher table

Posted on 2013-01-07
3
295 Views
Last Modified: 2013-02-16
I need to write a script that will process a few MySql queries, store the results, then write the results to another MySql table.  I then need this to run on my CentOS server as a cron job daily.

Thanks
0
Comment
Question by:ipconfig610
  • 2
3 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 38752515
Can you be more specific?  What are your queries?  Store the results where, besides the other MySQL table?  What's the structure of the tables involved?

If the requirement is simple enough and you really need to just store to another table it may be possible to just have an SQL file that contains something like:

insert into new_table (col1, col2, col3, col4) select a, b, c, d from table1 join table2....

Then just call have cron call: mysql -u username --password=yourpassword databasename < file.sql (or have put that in a shell script and have cron call that instead)
0
 

Author Comment

by:ipconfig610
ID: 38752565
I basically want to run a few SELECT COUNT statements and store the results in another table since the counts change daily.

Example:
SELECT count(*) from database1.extensions;

I then want to write the count result to another table.  The other table will only have 3 fields, company name, date, and the result from the SELECT.
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 38753252
Where will you get company name?  It would really help if you provide all the details.  Assuming that's static, you can create an sql file maybe named do_counts_for_days.sh.sql that contains this:

insert into stats_table (`company_name`, `date`, `result`) select 'company a', curdate(), count(*) from database1.extensions;
insert into stats_table (`company_name`, `date`, `result`) select 'company a', curdate(), count(*) from database1.somewhere_else;
insert into stats_table (`company_name`, `date`, `result`) select 'company a', curdate(), count(*) from database1.another_source;

Open in new window


Then create an executable file maybe named do_counts_for_days.sh that contains this:

/usr/bin/mysql -u username --password=thepassword database_name < do_counts_for_days.sql

Open in new window


(replace /usr/bin/mysql with your actual full path to mysql client)

Then call do_counts_for_days.sh from cron
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

813 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

12 Experts available now in Live!

Get 1:1 Help Now