Jump to content

MYSQL Query Across Multiple Columns and Tables


koopkoop

Recommended Posts

I'm really new at MySQL and having a tough time with a query problem across multiple coulmns and tables:

 

I'm trying to create a "easy to use" search box on an e-commerce website. When a search term is plugged in, it'll search the "Products" table in columns "product names", "product ID numbers" and "product categories". If the term matches in any of those columns, it'll then pull the image location from the same table and then pull pricing information from a second "price" table based on the common product ID. The search results are not as expected though. If I search for a term that I know matches (eg. a productID of "123"), it'll return a huge amount of hits with nothing but the same product repeating over and over.

 

I troubleshot it by slowly eliminating terms from my query and it turns out it's the "OR"s that are fouling the search. I know this fixes it, but I don't understand why. Obviously, I'm using the wrong query string, but what do I replace it with to get the functionality I described above? Queries below:

 

 

BROKEN QUERY:

 

SELECT products.itemnumber,products.name,products.midimagename,prices.bestprice

FROM products,prices

WHERE products.category LIKE '%fall%' 

OR products.itemnumber LIKE '%fall%'

OR products.name LIKE '%fall%'

AND products.itemnumber = prices.itemnumber

 

 

 

 

SEMI-FIXED QUERY (It now doesn't do what I need, but at least it doesn't repeat the same search hit over and over):

 

SELECT products.itemnumber,products.name,products.midimagename,prices.bestprice

FROM products,prices

WHERE products.category LIKE '%fall%' 

prices.itemnumber

Link to comment
Share on other sites

You should probably use () to group our conditions in a logic way. It seems you might want it like this:

 

SELECT products.itemnumber,products.name,products.midimagename,prices.bestprice

        FROM products,prices

        WHERE

          (products.category LIKE '%fall%'

          OR products.itemnumber LIKE '%fall%'

        OR products.name LIKE '%fall%')

        AND products.itemnumber = prices.itemnumber

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.