从包含字符串的数据库中检索最接近的匹配字符串

时间:2013-09-10 10:33:20

标签: sql string search approximate

我有一个数据库,其中有一个TEXT类型的“Fruits”列;例如,如果我有两个字符串

1. Apple %d Mango %d Banana %d Guava %d Peach %d
2. Grapes %d Plum %d Pomegranate %d Apple %d

我想首先返回字符串1,然后返回字符串2。 如果我的查询字符串是

Apple 4 Mango 6 Banana 7 Guava 8 Peach 2

如果需要,我可以从存储的字符串中删除%d,但在查询字符串中,数字将存在; 知道我该怎么办?

1 个答案:

答案 0 :(得分:0)

你说你可以从字符串中删除%d,从查询字符串中删除数字,你可以使用REPLACE函数。

@str is the query string.

REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (@str, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', '')

这将替换所有数字,然后您可以进一步查询您的逻辑。