?
Solved

Help with search filtering query

Posted on 2011-03-06
5
Medium Priority
?
432 Views
Last Modified: 2012-05-11
I am looking for the best ways to do a search, but to also have keywords to exclude to really filter down results

Here is an example, I am looking for 'prince of persia', but I want to make sure that all the titles do not have any form of minifigure in it.

select * from products
where item_title like '%prince of persia%'
and item_title not like '%minifigure%'
or item_title not like '%minifig%'
or item_title not like '%mini-figure%'


Is there a better way to do this?  This started bringing back results that didn't have 'prince of persia' at all!!
0
Comment
Question by:theideabulb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35048938
select * from products where item_title like '%prince of persia%' and (item_title not like '%mini%figure%' or item_title not like '%minifig%')
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35048962
If you might have 'mini-fig' then use
select * from products where item_title like '%prince of persia%' and (item_title not like '%mini%figure%' or item_title not like '%mini%fig%')
0
 

Author Closing Comment

by:theideabulb
ID: 35048974
Wow, the parenthese made a huge difference.  Why does that make such a difference?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35049332
Because the OR condition is evaluated same level as the AND condition and here you get the overall result by evaluating the first and the second conditions, then their result is evaluated against the third condition and so on. If there is one OR condition as True then it suppresses the AND.

To make sure the result is from the main condition which is to be having 'prince of persia' in the field, we separate the AND to be alone and the rest of OR's evaluated together and then compared with the AND at the end.

So without parentheses it turns out to be:
select * from products
where item_title like '%prince of persia%'     (False)
and item_title not like '%minifigure%'            (True)
or item_title not like '%minifig%'                    (True)
or item_title not like '%mini-figure%'              (False)

False and True or True or False   ->   False or True or False   ->   True or False   ->   overall = True

With parentheses:
select * from products
where item_title like '%prince of persia%'     (False)
and (item_title not like '%minifigure%'            (True)
or item_title not like '%minifig%'                    (True)
or item_title not like '%mini-figure%')              (False)

False and (True or True or False)   ->   False and (True)   ->   overall = False
0
 

Author Comment

by:theideabulb
ID: 35050474
Thanks for the sql tip.  That will really help me out.  Good example, you should right a book :)
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question