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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:29:09+00:00 2026-05-23T01:29:09+00:00

I have 1 input field for a user to type in either a business

  • 0

I have 1 input field for a user to type in either a business name, city and state, or zip code. I want to be able to pull back the correct data, but right now i’m getting incorrect results.

For example, if someone searches for “ERICS”, it returns fine, but if someone searches for “ERICS ROCHESTER MN”, it returns all of the results from ROCHESTER, MN. I want it to return no results in that case.

By the way, I am parsing out the data so i’m passing in up to 4 values in my query. business name, city, state, zip.

How can I modify my query so that it will give me the correct results? Can I somehow check in the query which variables are not null?

Schema

Business

business_id | name   | city        | state | zip
1             TOMS     ROCHESTER     MN      55906
2             BILLYs   MINNEAPOLIS   MN      55555
3             ERICS    LAX           WI      11111

Rating

rating_id | rating
1           GOOD
2           BAD

business_rating

br_id | business_id | rating_id
1       1             1
2       1             2




select b.business_id,  
       b.name, 
       b.city,
       b.state, 
       b.zip
       count(br.business_id) num_ratings, 
       round(avg(br.quality_id),2) quality_rating,   
       round(avg(br.friendly_id),2) friendly_rating,   
       round(avg(br.professional_id),2) professional_rating
from business b
   Left Join business_rating br
       On br.business_id = b.business_id
   Left Join rating r 
       On r.rating_id = br.quality_id
       And r.rating_id = br.friendly_id
 where (upper(b.business_name) like '%ERICS%'
        and upper(b.city) like '%ROCHESTER%'
        and upper(b.state) like '%MN%'
        and upper(b.zip) like '') 
 or (upper(b.city) like '%ROCHESTER%'
     and upper(b.state) like '%MN%'
     and upper(b.zip) like '')
 or (upper(b.city) like '%ROCHESTER%'
     and upper(b.state) like '%MN%')
 or (upper(b.zip) like '')
 group by id
  • 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-23T01:29:09+00:00Added an answer on May 23, 2026 at 1:29 am

    Notice your WHERE clause – you’re asking for a four-part match OR a three-part match, etc. That’s why a three-part match (city, state, zip) is being returned.

    The best way to handle this is to ship it out to a real search engine, e.g. solr.

    Since you didn’t ask for the best way, here’s my answer to your question:

    Write 4 queries.
    

    One query for one-part requests, one query for two-part requests, etc. Have your code branch (or overload) based on how many parameters were provided.

    This is assuming that, if there were no “ERICS”, you would want to return no results, rather than showing some other bar in Rochester.

    Oh, and don’t apply functions (e.g. UPPER) to your columns – the engine will not be able to use your indexes to satisfy the query. Of course, using unanchored LIKE patterns will also preclude the use of indexes. Really, seriously, try solr.

    Or, try this…

    This query creates a *max_score* based on the number of fields that you’ve provided as non-null.

    It calculates the number of columns that match the input, which is the score.

    Then it only shows those rows whose score is the max.

    set @NAME = "erics";
    set @CITY = "rochester";
    set @STATE = "MN";
    
    select id,
    case when @NAME is not null then 1 else 0 end +
      case when @CITY is not null then 1 else 0 end +
      case when @STATE is not null then 1 else 0 end +
      case when @ZIP is not null then 1 else 0 end as max_score,
    case when @name is not null and name like concat("%", @NAME, "%") then 1 else 0 end +
      case when @city is not null and city like concat("%", @CITY, "%") then 1 else 0 end +
      case when @STATE is not null and state like concat("%", @STATE, "%") then 1 else 0 end +
      case when @ZIP is not null and zip like concat("%", zip, "%") then 1 else 0 end as score
    from business
    having score = max_score;
    

    Now, please go install solr.

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

Sidebar

Related Questions

I have an input field on my page where the user will type in
I have an input edit field where the user can enter data. I want
I have an input field whose name is an MD5 string e.g.: <input type=hidden
I have the following input field for which i want to pull suggestions when
I have an input field that is by default set to type=text so that
I have an input field which has a placeholder text. I want one word
I have an input field and I want to limit it to alphanumerical (A-Z,
I have an input field in a form. Upon pushing submit, I want to
I have a form field that uses some Javascript to format user input 'price'
I have a text input field which i have made readonly so the user

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.