Note that there are some explanatory texts on larger screens.

plurals
  1. POMy SQL search query not distinguishing a column property?
    primarykey
    data
    text
    <p>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. </p> <p>The two groups displayed are "contacts" and "customers". Customers are contacts in the database that have been given a contact number that is <strong>NOT</strong> zero!</p> <p>This is a normal query which does not use any <strong>LIKE</strong> statements within the query.</p> <pre><code>$contacts = $db-&gt;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-&gt;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 "); </code></pre> <p>It works fine and displays results into their correct categories.</p> <p>Like so</p> <p><img src="https://i.stack.imgur.com/KnJoh.png" alt="enter image description here"></p> <hr> <p><strong>However</strong> My new query which is this</p> <pre><code>$contacts = $db-&gt;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-&gt;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"); </code></pre> <p>Displays all results in both lists even if they are <strong>NOT A Customer</strong></p> <p>Screenshot:</p> <p><img src="https://i.stack.imgur.com/8LFXU.png" alt="enter image description here"></p> <p>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?</p> <p>To summarise I want to do this:</p> <ul> <li>Compare with search string</li> <li>If any contacts match the search string put them in the contacts query result</li> <li>If any customers match the search string put them in the customers query result.</li> </ul> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload