I’ve looked around and can’t seem to quite find a solution that meets my needs, so I’m hoping you can help. (I’m somewhat new to MySQL, though I do have a good deal of SQL Server and Oracle experience to draw from.)
I have a database with several tables.
The first table is a definition table that stores basic information and is called cp_def:
cid (pkey) status
-------------------
10001 0
10002 1
Then, for each record in the cp_def table, there is a corresponding cp_[cid] table (where [cid] is the cid value from the cp_def table). Example:
table: cp_10001
id(pkey) code date_issued date_expired
-------------------------------------------------
1 ABC123 2011-06-23 2011-06-30
2 CYG124 2011-06-23 2011-06-30
table: cp_10002
id(pkey) code date_issued date_expired
-------------------------------------------------
1 CAC126 2011-06-23 2011-06-30
2 VGC254 2011-06-23 2011-06-30
I need to run a dynamic query every day (as a part of a shell script and cron job) to output the code and date_expired column values for ALL records in all cp_[cid] tables, where the date_issued value is the previous day’s date.
I have a query working (see below). However, every time I add a new cp_[CID] table, it requires me to manually append another UNION ALL statement to end of the query.
SELECT code, date_expired
FROM mydatabase.cp_10001
WHERE DATE(date_issued) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
UNION ALL
SELECT code, date_expired
FROM mydatabase.cp_10002
WHERE DATE(date_issued) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
My ultimate goal is to programmaticly generate the above query, at the time it’s executed, so that every time a new cp_[CID] table is added to the database, it doesn’t require manual intervention.
I tried using a view. However, it still seemed like adding a table was going to require a manual ALTERING of the view, to account for the new table.
Given that each cp_[CID] table is listed in the cp_def table (as well as the INFORMATION_SCHEMA table), I was thinking that I could perform some sort of loop to build a dynamic query, but I haven’t quite figured it out. In the interest of keeping this short, I’ll avoid posting my failed attempts at looping through the cp_def table to create a dynamic query.
Any assistance you could provide in pointing me in the right direction for crafting such a query would be greatly appreciated. If I’ve overlooked something, I do apologize. I’ve tried search for exactly what I’m looking for with many different phrases, to no avail.
I’d prefer to have one table with a structure like this (instead of many cp_####) –
But for you design, try this query –
This query generates a select statements with UNION ALL clauses, then execute generated query with prepared statements.
EDIT