Solved

Help with search filtering query

Posted on 2011-03-06
5
402 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
  • 3
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now