Link to home
Start Free TrialLog in
Avatar of judsonmusic
judsonmusicFlag for United States of America

asked on

sql alertative for in clause

I have a table that has a column called categories.

in this column you can have 'women, shirts, dresses' or 'men, pants'

You get the idea...

Anyway, my question is when  user searches for men, if I do

select *
from items
where categories like '%men%'

it pulls back anything with men and women in it. because of the word "men"

how can I do something like ( my logic between [] )

select *
from items
where [categories has the whole exact word "men" in it.]

Thanks

Judson
Avatar of Christopher Gordon
Christopher Gordon
Flag of United States of America image

Maybe put in some additional spaces:
select *
from items
where categories like '% men %'
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I originally thought of the spaces first, but if he's got 'pants, men, kids'

that becomes

' pants, men, kids '

and won't return properly.  The proper way is to split it into a table and then look within the table.
Avatar of judsonmusic

ASKER

ged325 can you explain furthur?
Judo:

I provided the answer in my previous post.

The function (in the code window) is a stand alone function which will split a string into a table.

(just copy and post the code into sql and execute).


You can then use that to split a delimitted string.

For instance

select * from fn_txt_split('1;2;3;4;5;',';')
returns
1
2
3
4
5

and
select * from fn_txt_split('a.b.c.d','.')
a
b
c
d



Since you have a comma delimited string, you can pass that to the function.  It will give you a table with 1 row per value.

Final query would look like:

select *
from items
where 'men' in  (select replace(item, ' ', '') item from fn_txt_split(categories, ','))



This doesn't handle your % search.  Do you still need to do that?
select *
from items
where 'men' in  (select replace(item, ' ', '') item from fn_txt_split(categories, ','))

what is this doing? what is item???
jsut to be clear, I have a link on a web page called "men" is passes a argument into a sql function in coldfusion which is like this(obviously this is not correct syntax below)

select *
from items
where [categories has the whole exact word "men" in it.]

so it will only pull back records like categories = 'men, shirts, pants' because that list has the word 'men' in it.
yep.  The the split will work.

item is coming from fn_txt_split

It is the only returning from that function.

I was just removing any spaces . . . I forget if the split trims for you automatically.

so in the case of
' men ,  pants'
it will still return the correct value.
so I run the function, then how would I pass all the values coming back from select * from items where category contains "mens"????

I dont think you have made it very clear on how to search each row for the category column.

Thanks

Judson
actually your function works nicely! one problem though, how do I install this function on my hosting accounts server? or do I just run it each time in my <cfquery> tag

can you explain how that works? When you run it once, does it stay in the server indefinitely?

Thanks

Judson
The query to create the function is a 1 off run, as the function will be created in your database and then available for use until the database is dropped.
Thats awesome. one small issue though. Some of my lists (categories) may have

'items for women, shirts, dresses, etc'

The way your function is now wont deal with that so I changed to this:

'women' in  (select item from fn_txt_split(replace(categories, ' ', ','), ','))

that way it will search all words :)

Thanks for your help!

Judson
Anytime.  Best of luck.
The correct way would still be to normalize this out to a second table.
Using this replace or like function is guaranteed to be a performance wreck when you have enough items.

Also you mention there is a link on a site somewhere that fills in this parameter, so watch out for SQL injection.

If you ask me it's an accident waiting to happen.
like function is guaranteed to be a performance wreck when you have enough items.
Too true.  It is fine now with a few hundred rows, wait until you have 100K rows and I suspect we will see you here again.

If you ask me it's an accident waiting to happen.
But, it is also an invaluable learning lesson.
In coldfusion I am using a local component/ method which only allows specific arguments enclosed in cfquery param tags to avoid SQL injection attacks. I am not using likes. I am using that wonderful split function. The site is in progress but it's www.feelthevibration.com beware its an adult novelty website ;) but I  a web developer and it's been a fun project!

It's best  viewed in safari or chrome to get the 3d effect!

Judson