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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:04:47+00:00 2026-06-14T09:04:47+00:00

I need to make a query in MySQL. But the query needs to be

  • 0

I need to make a query in MySQL.
But the query needs to be made dinamically with options choosen by the user.

The options are Day, Month, Year & SellerID.

A user will choose if he want to view the sales history from sellers by, days, months and years.

But, let’s say he wants to see all sales on March, 2012. He will leave Day in blank and receive all sales from March 01 till March 31…

And maybe he leaves the option Month in blank, and set Day=15 and Year=2012, he will get the sales history from the whole year 2012, and the 15th day of each month only.

My program needs to build a query that covers exactly what he wanted to view.

The question is:
Do I really need to use thousands of IF on my code? Isn’t there a better way to do that?

if( ($day==0) && ($month==0) && ($year>0) ){

    $query.="`date`>='{$year}-01-01' AND `date`<='{$year}-12-31'";

}elseif( ($day==0) && ($month>0) && ($year>0) ){

    $query.="`date`>='{$year}-{$month}-01' AND `date`<='{$year}-{$month}-31'";

}elseif{

etc...
  • 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-14T09:04:49+00:00Added an answer on June 14, 2026 at 9:04 am

    You could preprocess the values before using them:

    $year  = $year  > 0 ? (int) $year  : 2012;
    $month = $month > 0 ? (int) $month : 1;
    $day   = $day   > 0 ? (int) $day   : 1;
    
    $query .= sprintf("`date` BETWEEN '%4d-%2d-%2d' AND '%4d-%2d-%2d'",
        $year, $month, $day,
        $year, $month, 31
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a mySQL query and am not sure what the format
How can I make a mysql dump for table from a query? I need
So I need to make 3 tabs with query (got this part down), but
I am making a multi-table fulltext query.But I met some question. I need make
I need to add an if/else stament inside a MySQL query but can't get
I am using this script to display MYSQL data, but I need to make
I need to make a query output Using table, I want the following report,
I need to make a query like this: SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId
I need to make a query to one of Google's services. I read this
I'm playing with GAE, and need to make a query with something like 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.