Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
The viewer will learn how to dynamically set the form action using jQuery.

747 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

10 Experts available now in Live!

Get 1:1 Help Now