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 8820965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:44:17+00:00 2026-06-14T05:44:17+00:00

I am working on some reports and I am trying to create a query

  • 0

I am working on some reports and I am trying to create a query that would select a different query depending on the date selected, this is a simplify version of what I am trying to achieve

SELECT *
FROM
    IF(YEAR('###date start###'=2011),table_2011,table)
WHERE
    date_start BETWEEN '###date start###' AND '###date end###'

###date start### and ###date end### are parameters entered by the user.

I cannot use the language to select the different table depending on the date as this is only an example and I might have other parameters passed that would select a different table.

I am thinking that I will need to create a new preg_replace and force the person that create the queries to add something like ###if(test,val2,val2)### but if mysql can already do this I would not like to re-invent the hot-water.

Thank you

EDIT

While waiting for a better solution, I changed the “search replace” function from a simple:
###search### => value (where the ###search### was showing to the end user as a field to manually enter), to also accept a new field formatted like
LLLlabel[option:value,option:value,option:value,....]LLL

Not the best solution as I wish I could do it with MySQL only as this solution is only an annoyance to the end user to have to select an extra parameter.

Please keep in mind that the question is only a VERY simplified version of the query, the query can be part of subqueries, or unions, or joins and not only have a time separation

  • 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-06-14T05:44:18+00:00Added an answer on June 14, 2026 at 5:44 am

    A stored procedure with the following will work:

    DELIMITER //
    
    CREATE PROCEDURE example(date_start DATETIME, date_end DATETIME)
    BEGIN
        SET @table = IF(YEAR(date_start)=2011,'table_2011','table');
    
        SET @sql = CONCAT(
            "SELECT * FROM ",
            @table,
            " WHERE date_start BETWEEN ",
            QUOTE(date_start),
            " AND ",
            QUOTE(date_end)
        ); 
    
        PREPARE stmt FROM @sql; 
        EXECUTE stmt; 
        DEALLOCATE PREPARE stmt; 
    END;
    //
    
    DELIMITER ;
    

    and call it via:

    CALL example('###date start###', '###date end###');
    

    or if your data is in MyISAM tables, you could set up a MERGE table:

    CREATE TABLE all_tables (...) ENGINE = MERGE UNION=(table_2011, table);
    
    SELECT *
    FROM
        all_tables
    WHERE
        date_start BETWEEN '###date start###' AND '###date end###'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a Crystal Reports export with different localization. For this example, I'm
I'm working some code that inserts csv rows into an SQLite database using Python.
Working on some tweaks for a build script, I noticed that the output from
Whilst working on some generally horrible Javascript code this morning, I came across the
I'm pretty new to threads and would like some insight. I'm trying to get
I'm trying to create a report of some data. The data has be grouped
I have some troubles with my database. I am trying to create a subreport
I'm trying to write some Python code that will traverse each directory in the
I'm working in Rails and Activerecord and trying to merge some data from related
I'm working on a Rails application that's kind of like a blog. Users create

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.