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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:46:25+00:00 2026-05-29T20:46:25+00:00

I have two seperate queries for my database which return search results for two

  • 0

I have two seperate queries for my database which return search results for two different types of data which are displayed seperately on the search results page.

The two groups displayed are “contacts” and “customers”. Customers are contacts in the database that have been given a contact number that is NOT zero!

This is a normal query which does not use any LIKE statements within the query.

$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id = 0 GROUP BY first_name, second_name, company_name, email, quote_name ");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id != 0 GROUP BY first_name, second_name, company_name, email, quote_name ");

It works fine and displays results into their correct categories.

Like so

enter image description here


However My new query which is this

$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id =0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id !=0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid  GROUP BY first_name, second_name, company_name, email, quote_name");

Displays all results in both lists even if they are NOT A Customer

Screenshot:

enter image description here

Im guessing there is something wrong in the new query in which It is not able to compare correctly if the column “customer_id” is not equal to zero and is displaying everything?

To summarise I want to do this:

  • Compare with search string
  • If any contacts match the search string put them in the contacts query result
  • If any customers match the search string put them in the customers query result.

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-29T20:46:27+00:00Added an answer on May 29, 2026 at 8:46 pm

    You need parenthesis around your LIKE statements. It’s getting to your WHERE clause, checks for customer_id=0 AND first_name LIKE '%$search%' and then the next it checks OR second_name LIKE '%$search' and it won’t matter if customer_id=0 because it’s only checking the one case now. By putting the parens around it, it will check customer_id=0 and then all the LIKES.

    $contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id =0 AND (first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%') AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
    $customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id !=0 AND (first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%') AND contact.id = contact_quote.cid  GROUP BY first_name, second_name, company_name, email, quote_name");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that pulls together aggregate data from two different tables. I
I have a php page that pulls data from a mysql database based on
Suppose you have two seperate ASP.NET Web Application projects that both need to use
I have had this problem w/ two seperate WYSWYG editors in my rails application
I have two physically-separate MySQL databases on which I have to run a single
So I have two methods. Method one adds a row to my database. Method
I have a PostgreSQL database with two tables named user and group. They have
i have 2 tables(result of two separate SQL queries and this result will be
I have a web solution (in VS2010) with two sub-projects: Domain which holds the
Suppose I have a database which contains blog posts which have tags and I

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.