2 table SQL Query weird results

Posted by javArc on Stack Overflow See other posts from Stack Overflow or by javArc
Published on 2010-06-11T18:18:54Z Indexed on 2010/06/11 18:22 UTC
Read the original article Hit count: 144

Filed under:

Ok this is driving me nuts, I need to write an SQL query that will grab product information from 2 tables. The first table 'products' contains the productId, productname, quantityperunit and unitprice. Now I can search by productname and categoryname individually, but when I try to combine the 2 I get crazy results, Here's the query:

"SELECT DISTINCT productId, productname, quantityperunit, unitprice FROM products pr,
categories ca WHERE pr.categoryID = ca.categoryID AND ProductName LIKE '%" + searchTerm + "%'
OR CategoryName LIKE '%" + searchTerm + "%'

excuse the java style in there, here it is formatted better:

SELECT DISTINCT productId, productname, quantityperunit, unitprice FROM products pr,
categories ca WHERE pr.categoryID = ca.categoryID AND ProductName LIKE 'Tofu'
OR CategoryName LIKE 'Tofu'

any help would be appreciated.

© Stack Overflow or respective owner

Related posts about mysql