Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-07
3
Medium Priority
?
301 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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…
Suggested Courses

926 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