我需要从具有WHERE
多种LIKE
组合条件的表中获取行,
我的意思是,例如,我description
在表中有一个字段,它有两行
1. 'abc def hij klm'
2. 'opq rst uvw xyz'
我的搜索文本是abc rst
. 所以我需要一个LIKE
条件必须产生两行的查询,因为第一行包含字符串abc
,第二行包含rst
。我知道我可以使用OR
in between twoLIKE
条件,但是如果搜索文本太长,因为它有更多的单词,我该怎么办?
我需要从具有WHERE
多种LIKE
组合条件的表中获取行,
我的意思是,例如,我description
在表中有一个字段,它有两行
1. 'abc def hij klm'
2. 'opq rst uvw xyz'
我的搜索文本是abc rst
. 所以我需要一个LIKE
条件必须产生两行的查询,因为第一行包含字符串abc
,第二行包含rst
。我知道我可以使用OR
in between twoLIKE
条件,但是如果搜索文本太长,因为它有更多的单词,我该怎么办?
This can be a bit painful, but one way to do it is with like and substring_index()
where concat(' ', description, ' ') like concat('% ', substring_index(@SearchString, ' ', 1), ' %') and
concat(' ', description, ' ') like concat('% ', substring_index(substring_index(@SearchString, ' ', 2), ' ', -1), ' %') and
concat(' ', description, ' ') like concat('% ', substring_index(substring_index(@SearchString, ' ', 3), ' ', -1), ' %') and
concat(' ', description, ' ') like concat('% ', substring_index(substring_index(@SearchString, ' ', 4), ' ', -1), ' %')
The nested substring_index()
return the nth item of the list.
If the codes you are looking for a real codes just glommed together in the description
field, then you have bad database design. You need an association table with a single row for each code.
If the description is really a text description, then you might really want full text indexing. This would solve your problem using match . . . against
.