Solved

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

Posted on 2013-01-07
3
296 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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