douwu0335 2013-02-11 12:05
浏览 78
已采纳

MySql查询质询 - 用空格返回结果?

I have a MySql database with some rows as follows:

ID    DESC
 1    This is my bike
 2    Motorbikes are great
 3    All bikers should wear helmets
 4    A bike is great for exercise
 5    A. Top. Bike.

What I want to do is return the rows with whitespace surrounding the search term, OR the term being at the end or beginning of the description.

For example,

"SELECT * FROM `mytable` WHERE `desc` LIKE '%bike%'"

Will return all rows. But,

"SELECT * FROM `mytable` WHERE `desc` LIKE '% bike %'

Will only return row 4.

What I really want is a reliable way to return rows 1, 4 and 5, i.e. where the search term is sorrounded with anything BUT chars A-z, 0-9. Any ideas? Is this even possible with MySql?

Thanks!!

  • 写回答

4条回答 默认 最新

  • dongshui2254 2013-02-11 12:15
    关注

    You should start reading about MySql RegEx.

    Sample Code.

    SELECT * FROM table WHERE field_name REGEXP PATTERN;

    More Specific

    details Table

    ID      NAME
    1       Dipesh
    2       Dip
    3       Dipe
    4       DiDi
    5       Di
    
    SELECT * FROM details WHERE NAME REGEXP '^Di$';

    Result

    NAME -> Di

    SELECT * FROM details WHERE NAME REGEXP 'Di$';

    Result

    NAME -> DiDi , Di

    SELECT * FROM details WHERE NAME REGEXP '^Di';

    Result

    NAME -> Dip, DiDi, Di

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?