0

我需要从具有WHERE多种LIKE组合条件的表中获取行,

我的意思是,例如,我description在表中有一个字段,它有两行

1. 'abc def hij klm'
2. 'opq rst uvw xyz'

我的搜索文本是abc rst. 所以我需要一个LIKE条件必须产生两行的查询,因为第一行包含字符串abc,第二行包含rst。我知道我可以使用ORin between twoLIKE条件,但是如果搜索文本太长,因为它有更多的单词,我该怎么办?

4

1 回答 1

0

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.

于 2013-07-27T12:12:48.067 回答