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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:30:20+00:00 2026-05-30T05:30:20+00:00

Is there a more succinct way to write the below code? $myQuery = SELECT

  • 0

Is there a more succinct way to write the below code?

$myQuery = "
   SELECT * FROM `rooms`
    WHERE (
              `Facility1` IN ($inList)
           OR `Facility2` IN ($inList)
           OR `Facility3` IN ($inList)
           OR `Facility4` IN ($inList)
           OR `Facility5` IN ($inList)
           OR `Facility6` IN ($inList)
           OR `Facility7` IN ($inList)
           OR `Facility8` IN ($inList)
           OR `Facility9` IN ($inList)
           ) AND `Location` LIKE '".$Location."%'
             AND `RoomType` LIKE '".$RoomType."%'
    ORDER BY CONVERT(`Capacity`, SIGNED)
";
  • 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-30T05:30:21+00:00Added an answer on May 30, 2026 at 5:30 am

    In order to make this more compact, you could write code to build the query for you:

    $fac_array=array();
    for ($i=1;$i<=9;$i++){
        array_push($fac_array, "Facility$i in (\$inlist)");
    }
    $facility_condition = implode(" OR ",$fac_array);
    $full_query = "SELECT * FROM rooms WHERE (".$facility_condition.") AND....";
    

    But really, the issue with a query like the one you show is not about the code, but about the structure of your data model, which is not “normalized”.

    “Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency.”. You have 9 “Facility” fields, and this gives you three problems (not mentioning the difficulty of building queries):

    1. If you ever have more than 9 facilities, you need to change your database schema and all your queries.
    2. If you sometimes use less than 9 facilities for each record, you’re wasting space.
    3. If you change a facility’s information, you’d have to go through your database and change the data in each record that contains it.

    The issue here is that the “Facility” should be an “entity” in itself, and as such, should be contained in its own table; other tables would just “refer” to the canonical Facility table. This avoids the problems I mention through a number of well-known relational database techniques.

    I sugges you read up on database normalization and consider reworking your database’s structure to conform to having a Facility table, and (as I deduce you will need from looking at the query) an “intermediate” facility_room table which would map facilities to rooms.

    Here’s a place to get started: http://en.wikipedia.org/wiki/Database_normalization

    and here: http://www.devshed.com/c/a/MySQL/An-Introduction-to-Database-Normalization/

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

Sidebar

Related Questions

Is there more elegant way of escaping array elements: mysql_query(SELECT * from mytable WHERE
I was wondering if there is a cleaner (more succinct) way to do what
Is there a more succinct/correct/pythonic way to do the following: url = http://0.0.0.0:3000/authenticate/login re_token
Is there are more succinct or Rubyesque way of writing this: if ( variable
Is there a more succinct way to define a class in a namespace than
Is there a more efficient way to convert an HTMLCollection to an Array, other
Would there a more elegant way of writing the following syntax? Thread t0 =
If there is more than one way, please list them. I only know of
Is there a more efficient way to clamp real numbers than using if statements
Is there are more efficient way than the following for selecting the third parent?

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.