tags:

views:

89

answers:

3

I will start by saying that I don't think what I want can be done, but that said, I am hoping I am wrong and someone knows more than me. So here is your chance... Prove you are smarter than me :)

I want to do a search against a SQLite table looking for any records that "are similar" without having to write out the query in long hand.

To clarify this is how I know I can write the query:

select * from Articles where title like '%Bla%' or category like '%Bla%' or post like '%Bla%'

This works and is not a huge deal if you are only checking against a couple of columns, but if you need to check against a bunch then your query can get really long and nasty looking really fast, not to mention the chance for typos. (ie: 'Bla%' instead of '%Bla%')

What I am wondering is if there is a short hand way to do this?

*This next code does not work the way I want, but just shows kind of what I am looking for

select * from Articles where title or category or post like '%Bla%'

Anyone know if there is a way to specify that multiple 'where' columns should use the same search value without listing that same search value for every column?

Thanks in advance!

+4  A: 

I think considering you are using sqlite you should consider using Full Text Search. It'll make what you're attempting to do more efficient.

http://www.sqlite.org/cvstrac/wiki?p=FtsUsage

Nissan Fan
That is even better then what I was looking for! Thanks!
kdbdallas
One problem I just found with this while trying to implement it, is that you can not have any indexes on the table (besides the built in full text search ones) When trying to run: CREATE UNIQUE INDEX "articleID" ON "Articles" ("articleID"); It gives the error: "Virtual tables may not be indexed" This means while speeding up the full text search it will slow down all of my normal queries as it will have to do a full table scan. hrm. Normal queries will be more important (in my case) so unfortunately I don't think this will work for me....
kdbdallas
A: 

The quickest thing I can think of, is to concatenate title, category and post, and see if the concatenated string matches '%Bla%'. To make this work reliably, use a separator like ',' when concatenating (else, you could have title 'Bl' and cagegory 'a').

I don't know off the top of my head, what the concatenation operator is called in SQL, and I'm not sure, if one exists in generic SQL or SQLite.

My idea is, that it could look something like:

select * from Articles where concat(title, ',', category, ',', post) like '%Bla%'
Chris Lercher
Here ya go: `title || '--' || category || '--' || post like '%Bla%'`
Nick Craver
A: 

Maybe you want to concatenate your fields into a single string and then LIKE against that?

select * from Articles where (title || ';' || category || ';' || post) like '%Bla%'

The semicolons in between are arbitrary and are there to ensure that "ROB" and "lance" don't bring back a false positive.

If you want to not have to provide the fields at all, that's a more complicated one...

LesterDove
Oddly enough, I just looked up the concatenation operator for SQLlite, and it's a double pipe. This makes my example unnecessarily confusing, maybe.
LesterDove
Chose a better delimiter. And I'm voting for Nissan's answer anyhow.
LesterDove