首页 > 解决方案 > mysql中的正则表达式来回答给定的查询

问题描述

我的 mysql 表有一个列结构,它转储由“;”分隔的各种属性 对于 html 文件的每个元素。

+-----------+------------------------------------+
| tag_id    | attributes                         |
+-----------+------------------------------------+
|  1        |class:block_22;id:toc_id_35;        |
|  2        |class:bloack_1000;id:12#4           |
+-----------+------------------------------------+

我想编写一个查询来选择具有

  1.id attribute that starts with #
  2.id attribute that ends with #
  3.id attribute that has # in between

第一个查询可以这样回答

SELECT * FROM table where attributes like "%id:#%";

我不确定如何处理其他两个查询。请给我一些有价值的见解。

标签: mysqlsqlregex

解决方案


这些模式应该做你想做的事:

where attribute regexp 'id:#'
where attribute regexp 'id:[^;]*#(;|$)'
where attribute regexp 'id:[^;]+#[^;]+'

推荐阅读