怎么通过REGEXP实现子集判断查询?

MySql help-doc 10年前 (2014-04-27) 1129次浏览 0个评论

标题可能描述的不清楚,我举个例子:

-- ----------------------------
-- Table structure for `test`
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `A` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES (""1"", ""1,2"");
INSERT INTO `test` VALUES (""2"", ""2,3,4"");
INSERT INTO `test` VALUES (""3"", ""2,3"");

有一个一直变量B=‘1,2,3’,我想通过REGEXP实现查询获取id分别为1和3的数据,因为我对正则表达式一窍不通,所以来请教如何实现?

为什么不是查id为 1 / 2 / 3的呢
5分
可以考虑用  instr   / find_in_set
正则的话就有点多余了  WHERE  “”[1,2,3]”” regexp ID
引用 2 楼 hxtgirq710 的回复:

可以考虑用  instr   / find_in_set
正则的话就有点多余了  WHERE  “”[1,2,3]”” regexp ID

这两个函数不能直接用啊,因为被查参数只能是单个值,如果要用还要先拆分循环变量B。

正则表达式应该怎么实现呢?求教各位前辈!

mysql> select * from test;
+----+-------+
| id | A     |
+----+-------+
|  1 | 1,2   |
|  2 | 2,3,4 |
|  3 | 2,3   |
+----+-------+
3 rows in set (0.00 sec)

mysql> set @B="1,2,3";
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test where instr(@B,A);
+----+------+
| id | A    |
+----+------+
|  1 | 1,2  |
|  3 | 2,3  |
+----+------+
2 rows in set (0.00 sec)

以上测试完全符合楼主提供的测试用例。所以说有时举例的确是一种学问,需要细心,耐心。

35分

mysql> select * from test;
+----+-------+
| id | A     |
+----+-------+
|  1 | 1,2   |
|  2 | 2,3,4 |
|  3 | 2,3   |
+----+-------+
3 rows in set (0.00 sec)

mysql> set @B="1,2,3";
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test
    -> where concat(A,",") regexp concat("^((", replace(@B,",","|"), "),)*$");
+----+------+
| id | A    |
+----+------+
|  1 | 1,2  |
|  3 | 2,3  |
+----+------+
2 rows in set (0.00 sec)

mysql>

.

引用 4 楼 ACMAIN_CHM 的回复:
mysql> select * from test;
+----+-------+
| id | A     |
+----+-------+
|  1 | 1,2   |
|  2 | 2,3,4 |
|  3 | 2,3   |
+----+-------+
3 rows in set (0.00 sec)

mysql> set @B="1,2,3";
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test where instr(@B,A);
+----+------+
| id | A    |
+----+------+
|  1 | 1,2  |
|  3 | 2,3  |
+----+------+
2 rows in set (0.00 sec)

以上测试完全符合楼主提供的测试用例。所以说有时举例的确是一种学问,需要细心,耐心。

我举得例子有点不妥,如果id为1的记录中A字段为1,3的话,instr就查不到这条记录了。

引用 5 楼 ACMAIN_CHM 的回复:

mysql> select * from test;
+----+-------+
| id | A     |
+----+-------+
|  1 | 1,2   |
|  2 | 2,3,4 |
|  3 | 2,3   |
+----+-------+
3 rows in set (0.00 sec)

mysql> set @B="1,2,3";
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test
    -> where concat(A,",") regexp concat("^((", replace(@B,",","|"), "),)*$");
+----+------+
| id | A    |
+----+------+
|  1 | 1,2  |
|  3 | 2,3  |
+----+------+
2 rows in set (0.00 sec)

mysql>

.

这个可就完全可以实现我没表达清楚的功能,只要A字段中的值在变量B中出现过就能查到。
多谢版主!

另外通过版主的例子我学习了一下MySQL正则表达式,查询所有A字段中包含变量C的子数字的记录也实现了:

set @C="2,4";
select * from test where concat(A,",") regexp concat(replace(@C,",",",([0-9]+,)*"), ",([0-9]+,)*");

所得结果为:

2 2,3,4

再次感谢版主!


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么通过REGEXP实现子集判断查询?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!