Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with search filtering query

Posted on 2011-03-06
5
Medium Priority
?
439 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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