SQLstatementtosearchforWordBoundary_MySQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SQLstatementtosearchforWordBoundary_MySQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2201字,纯文字阅读大概需要4分钟。
内容图文
![SQLstatementtosearchforWordBoundary_MySQL](/upload/InfoBanner/zyjiaocheng/581/3750509ddc7544dda6316b245918abbe.jpg)
Send this article by email
What is your name?
Please indicate below the emails to which you want to send this article: SQL statement to search for Word Boundary
Enter one email per line. No more than 5 emails.
In SQL, sometimes, you want to search a string field that contains a key, but as a whole word. So for example, if you search for ‘ word ‘, you do not want ‘ wordword ‘ to be included in the result. Suppose, we have the following data in the MySQL table test.
mysql> select * from test;+-------------------+| name|+-------------------+| word|| word ok || ok word || wordword|| wordword wordword || (word)(word)|| ,word,|+-------------------+7 rows in set (0.00 sec) |
mysql> select * from test;+-------------------+| name|+-------------------+| word|| word ok || ok word || wordword|| wordword wordword || (word)(word)|| ,word,|+-------------------+7 rows in set (0.00 sec)
So, if we use percentage symbol % to match one or a few characters, we can use like “%word%” to match, but this gives unnecessary matches.
mysql> select * from test where name like "%word%";+-------------------+| name|+-------------------+| word|| word ok || ok word || wordword| ---------------> incorrect match| wordword wordword | ---------------> incorrect match| (word)(word)|| ,word,|+-------------------+7 rows in set (0.00 sec) |
mysql> select * from test where name like "%word%";+-------------------+| name|+-------------------+| word|| word ok || ok word || wordword| ---------------> incorrect match| wordword wordword | ---------------> incorrect match| (word)(word)|| ,word,|+-------------------+7 rows in set (0.00 sec)
The correct method is to use the REGEXP and the [[:<:]] and [[:>:]] word-boundary markers:
mysql> select * from test where name regexp '[[:<:]]word[[:>:]]';+--------------+| name |+--------------+| word || word ok|| ok word|| (word)(word) || ,word, |+--------------+5 rows in set (0.01 sec) |
mysql> select * from test where name regexp '[[:<:]]word[[:>:]]';+--------------+| name |+--------------+| word || word ok|| ok word|| (word)(word) || ,word, |+--------------+5 rows in set (0.01 sec)
–EOF–
GD Star Rating
loading...
GD Star Rating
loading...
内容总结
以上是互联网集市为您收集整理的SQLstatementtosearchforWordBoundary_MySQL全部内容,希望文章能够帮你解决SQLstatementtosearchforWordBoundary_MySQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。