我有这张桌子。

+--------+----------------+
| id     | e_id           |
+--------+----------------+
| 164462 | " 3 1 2 4 6 "   |
| 164463 | " 185 1 2 4 6 " |
| 164464 | " 3 1 2 1140 6 "|
| 164465 | " 52 1 2 4 6 "  |
| 164466 | " 3 1 175 4 6 " |
| 164467 | " 3 1 2 4 6 "   |
| 164468 | " 3 1 2 4 6 "   |
| 164469 | " 3 1 2500 4 6 "|
| 164470 | " 82 1 2 4 6 "  |
| 164471 | " 3 1 2 4 6 "   |
| 164472 | " 3 1 50 4 6 "  |
| 164473 | " 3 1 2 4 6 "   |
+--------+-----------------+

I must find that rows that contains numbers between 175 and 2574, but numbers are separated just with [[:space:]].

I was tried to use mysql query like this:

 select *
   from table_name
   where e_id between REGEXP '[[:space:]]175[[:space:]]'
     and  REGEXP '[[:space:]]2574[[:space:]]';


...还有很多这样的组合,但是没有运气...

感谢帮助!:)

最佳答案

您永远不要在单个数据库列中有这种“列表”。在这种情况下,您应该具有另一个表来一次链接一个e_id

无论如何,如果边界不是动态的,这是解决您的问题的一种方法:创建一个正则表达式以匹配175和2574之间的每个数字:

SELECT *
FROM table_name
WHERE e_id REGEXP("[[:space:]](1(7[5-9]|[89]\d)|[2-9]\d{2}|1\d{3}|2([0-4]\d{2}|5[0-6]\d|57[0-4]))[[:space:]]")


正则表达式详细信息:


1(7[5-9]|[89]\d)将匹配175至199
[2-9]\d{2}将匹配200到999
1\d{3}将匹配1000到1999
2([0-4]\d{2}将匹配2000到2499
25[0-6]\d将匹配2500到2699
257[0-4]匹配2570至2574


该解决方案很丑,因为您的数据库很丑。
它适用于特定的边界,但是如您所见,如果更改这些边界,则需要重写另一个正则表达式。



您应该拥有的:

一个包含您的ID的表,另一个包含“ e_ids”的表,然后是第三个将两者链接在一起的表:

CREATE TABLE `elements` (
    `id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
);
CREATE TABLE `other_elements` (
    `e_id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`e_id`)
);
CREATE TABLE `link_table` (
    `id` INT NOT NULL,
    `e_id` INT NOT NULL,
    INDEX `id` (`id`),
    INDEX `e_id` (`e_id`),
    CONSTRAINT `FK__elements`
        FOREIGN KEY (`id`)
        REFERENCES `elements` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT `FK__other_elements`
        FOREIGN KEY (`e_id`)
        REFERENCES `other_elements` (`e_id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
);


然后,您将能够轻松获得想要的东西:

SELECT *
FROM elements e
JOIN link_table l
  ON l.id = e.id
WHERE l.e_id BETWEEN 175 AND 2574

10-04 19:40