开发者

Search a string to find which records in table are inside said string

Say I have a string.

Then I have a number of unique tokens or keywords, potentially a large number in a database.

I want to search and find out which of these database strings are inside the string I provide (and get the IDs of them).

Is there a way of using a query to search the provided string or must it be taken to application space?

Am I right 开发者_如何学Goin thinking that this is not a 'full text search'? Would the best method be to insert it into the database to make it a full text search?


look at the

  • position(token in LongString) function for postgresql
  • instr(oken,LongString) function for mysql.

Each returns the position of the token in the LongString. When that position is > 0 you have a match.

postgresql:

select st.string, tt.token 
  from tokentable as tt 
     , stringtable as st
 where position(tt.token in st.string) >0

mysql:

select st.string, tt.token 
  from tokentable as tt 
     , stringtable as st
 where instrr(tt.token ,st.string) >0

Please be aware that this is going to be slow and ressource hungry. If you need to do that kind of matching often, you might want to redesign your DB.

Why is this marked mysql and postgresql?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜