Solved

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

Posted on 2013-01-07
3
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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