MySQL exact word existence check in a table Field

别说谁变了你拦得住时间么 提交于 2019-12-04 12:29:36

Use this query

SELECT * FROM sometable WHERE somefield LIKE '% age %' 
OR somefield LIKE 'age %' OR somefield LIKE '% age'

You can use MySQL's regex engine and match the word boundary characters [[:<:]] and [[:>:]]

This will match age but not courage or wage or aged. It will also match the age in -age- or any other row where the text age is surrounded by non word characters. This means it will match age at the beginning and end of the string.

It is a little less complicated to read in my opinion than having three conditions. If returning matches like -age- is acceptable then you might consider this.

I have not tested the performance or portability of this query either so you might want to consider that as well.

SELECT * FROM sometable WHERE somefield RLIKE '[[:<:]]age[[:>:]]';

Try this

  1 SELECT * FROM sometable WHERE somefield LIKE '% age %' to search in middle of string

2 SELECT * FROM sometable WHERE somefield LIKE '% age' if word is at end of string

3 SELECT * FROM sometable WHERE somefield LIKE 'age %' if your word is at starting of the string 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!