简体   繁体   English

SQL喜欢带附加字符

[英]SQL like with additional characters

I'm doing a textual search on a MySql table and I want to find words that may end with certain characters. 我正在MySql表上进行文本搜索,我想找到可能以某些字符结尾的单词。

Here's an example. 这是一个例子。 The search is for the word clip . 搜索是单词clip I want my query to find the word clip, or clip? 我想查询找到单词clip,还是clip? but not the word clipping . clipping一词不行。

So far this is what I tried: 到目前为止,这是我尝试过的:

select * from product where title like '%clip%'

also finds the word clipping , which is not good. 还找到了clipping这个词,不好。

select * from product where title like '% clip %'

doesn't find clipping but also ignores the word clip when there's a comma right after it, clip, and that's also not good for me. 找不到clipping但是在clip后面紧跟一个逗号时,它会忽略clip一词clip,这对我也不利。

Is there a way to specify that the query would ignore letters, but include characters like , or . 是否有指定的查询会忽略字母,但包括像字符的方式,. or ? 还是? or basically any other character I choose? 还是我选择的其他任何字符?

You can use a regular expression that matches word boundaries: 您可以使用匹配单词边界的正则表达式:

... where title regexp '[[:<:]]clip[[:>:]]'

If you have a lot of data and find yourself doing lots of searches like this, it may be a good idea to create a full text index and then use match...against : 如果您有大量数据,并且发现自己在进行类似这样的大量搜索,则最好创建全文索引 ,然后使用match...against

... where match(title) against ('clip');

Use REGEXP . 使用REGEXP

where title REGEXP '.* clip[,.? ].*'

The following is a demo. 以下是一个演示。

mysql> select 'the paper clip is white' REGEXP '.* clip[,.? ].*';
+----------------------------------------------------+
| 'the paper clip is white' REGEXP '.* clip[,.? ].*' |
+----------------------------------------------------+
|                                                  1 |
+----------------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'the paper clip? is white' REGEXP '.* clip[,.? ].*';
+-----------------------------------------------------+
| 'the paper clip? is white' REGEXP '.* clip[,.? ].*' |
+-----------------------------------------------------+
|                                                   1 |
+-----------------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'the paper clipping is white' REGEXP '.* clip[,.? ].*';
+--------------------------------------------------------+
| 'the paper clipping is white' REGEXP '.* clip[,.? ].*' |
+--------------------------------------------------------+
|                                                      0 |
+--------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'the paper clipis white' REGEXP '.* clip[,.? ].*';
+---------------------------------------------------+
| 'the paper clipis white' REGEXP '.* clip[,.? ].*' |
+---------------------------------------------------+
|                                                 0 |
+---------------------------------------------------+
1 row in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM