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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:06:52+00:00 2026-05-26T06:06:52+00:00

I have a somewhat complex mySQL query I am trying to execute. I have

  • 0

I have a somewhat complex mySQL query I am trying to execute. I have two parameters: facility and isEnabled. Facility can have a value of “ALL” or be specific ID. isEnabled can have value of “ALL” or be 0/1.

My issue is that I need to come up with logic that can handle the following scenarios:
1) Facility = ALL AND isEnabled = ALL
2) Facility = ALL AND isEnabled = value
3) Facility = someID AND isEnabled = ALL
4) Facility = someID AND isEnabled = value

The problem is that I have several nested IF statements:

IF (Facility = 'ALL') THEN
  IF (isEnabled = 'ALL') THEN
    SELECT * FROM myTable
  ELSE
    SELECT * FROM myTable
    WHERE isEnabled = value
  END IF;
ELSE
  IF (isEnabled = 'ALL') THEN
    SELECT * FROM myTable
    WHERE facility = someID
  ELSE
    SELECT * FROM myTable
    WHERE facility = someID AND isEnabled = value
  END IF;
END IF;

I would like to be able to combine the logic in the WHERE clause using either a CASE statement or Conditional’s (AND/OR) but I am having trouble wrapping my head around it this morning. Currently the query is not performing as it is expected to be.

Any insight would be helpful!
Thanks

  • 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-26T06:06:52+00:00Added an answer on May 26, 2026 at 6:06 am

    You could do this…

    SELECT
      *
    FROM
      myTable
    WHERE
      1=1
      AND (facility  = someID OR Facility  = 'ALL')
      AND (isEnabled = value  OR isEnabled = 'ALL')
    

    However, this yields a poor execution plan – it’s trying to find one size fits all, but each combination of parameters can have different plans depending on data, indexes, etc.

    This means that it is better to build the query dynamically

    SELECT
      *
    FROM
      myTable
    WHERE
      1=1
      AND facility  = someID  -- Only include this line if : Facility  = 'ALL'
      AND isEnabled = value   -- Only include this line if : isEnabled = 'ALL'
    

    I know it can feel dirty to use dynamic queries, but this is a good corner case as to when then really can excel. I’ll go find a spectacularly informative link for you now. (It’s a lot to read, but it’s very worth learning from)

    Link : Dynamic Search

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat complex query with multiple (nested) sub-queries, which I want to
I have a somewhat complex regular expression which I'm trying to match against a
I have a somewhat complex SQL query that is pulling from a table of
I have a somewhat complex query that I want to use as a source
I have somewhat of a complex issue involving several different programs including redmine, MySQL,
So I am trying to do a somewhat complex query and am having trouble.
I have several somewhat separate programs, that conceptually can fit into a single project.
I have a somewhat complex data model in my iPad application (an OpenGL drawing
I have a somewhat complex custom user control, created in C# .NET, which is
I have an ObservableCollection of ChildViewModels with somewhat complex behaviour. When I go to

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.