Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6050381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:40:54+00:00 2026-05-23T07:40:54+00:00

I’ve looked around and can’t seem to quite find a solution that meets my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:40:55+00:00Added an answer on May 23, 2026 at 7:40 am

    I’d prefer to have one table with a structure like this (instead of many cp_####) –

    cp_properties:
    id(pkey), cid(foreign key to cp_def.cid) , code      date_issued    date_expired
    

    But for you design, try this query –

    SELECT GROUP_CONCAT(CONCAT('SELECT code, date_expired FROM mydatabase.cp_', cid, ' WHERE DATE(date_issued) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)') SEPARATOR '\r\nUNION ALL\r\n') FROM mydatabase.cp_def
    

    This query generates a select statements with UNION ALL clauses, then execute generated query with prepared statements.

    EDIT

    SELECT GROUP_CONCAT(CONCAT('SELECT code, date_expired FROM mydatabase.cp_', cid, ' WHERE DATE(date_issued) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)') SEPARATOR '\r\nUNION ALL\r\n') INTO @s FROM mydatabase.cp_def;
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.