Solved

Help with search filtering query

Posted on 2011-03-06
5
384 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

18 Experts available now in Live!

Get 1:1 Help Now