【问题标题】:Search a string to find which records in table are inside said string搜索字符串以查找表中的哪些记录在所述字符串内
【发布时间】:2010-04-05 04:08:18
【问题描述】:

假设我有一个字符串。

然后我有许多独特的标记或关键字,可能在数据库中很大。

我想搜索并找出我提供的字符串中的哪些数据库字符串(并获取它们的 ID)。

有没有办法使用查询来搜索提供的字符串或必须将其带到应用程序空间?

我认为这不是“全文搜索”是否正确? 最好的方法是将其插入数据库以使其成为全文搜索吗?

【问题讨论】:

    标签: sql mysql postgresql search full-text-search


    【解决方案1】:

    看看

    • position(token in LongString) postgresql 函数
    • mysql 的instr(oken,LongString) 函数。

    Each 返回标记在 LongString 中的位置。当该位置 > 0 时,您就有了匹配项。

    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
    

    请注意,这会很慢并且需要大量资源。如果您需要经常进行这种匹配,您可能需要重新设计您的数据库。

    为什么这里标记为mysql和postgresql?

    【讨论】:

    • 我想知道他们俩的策略。将字符串插入数据库然后进行全文搜索会更有效吗?假设你有一个字符串,它包含各个城市,你想找出字符串中有哪些城市。
    • 如果您预处理数据(识别所有城市并相应地插入),您的插入性能会降低。如果您在字符串中搜索数据库中的子字符串,那么您的搜索速度会变慢。您必须决定哪个用例必须更快。
    猜你喜欢
    • 2015-03-20
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多