我正在尝试使用 RLIKE 来匹配数据字符串后跟特定字符或字符串结尾 ($) 的位置。我只使用字符串字符 ($) 的结尾,或者只使用预期的字符,或者实际上是方括号内的任何预期字符集,得到预期的结果,但是一旦我进入方括号以获得预期的字符 OR 和结尾字符串字符,行尾不匹配。
这是一个例子:
SQL 数据:
CREATE TABLE test_table (id int auto_increment primary key, search_string varchar(8));
INSERT INTO test_table (search_string) VALUES("123456789");
INSERT INTO test_table (search_string) VALUES("1234567");
INSERT INTO test_table (search_string) VALUES("123456");
INSERT INTO test_table (search_string) VALUES("12345");
INSERT INTO test_table (search_string) VALUES("12345E");
对此数据的示例查询:
SELECT count(*) FROM test_table WHERE search_string RLIKE "56[7]";
# the above returns fine - 2 rows (first and second)
SELECT count(*) FROM test_table WHERE search_string RLIKE "56[7YE]";
# the above returns fine - 2 rows (rows 2 and 5) as expected
SELECT count(*) FROM test_table WHERE search_String RLIKE "56$";
# the above returns fine - 1 rows (the third) as expected as 6 is followed by end of string
SELECT count(*) FROM test_table WHERE search_string RLIKE "56[7$]";
# the above returns only 1 row and should be 2 (rows 2 and 3 - '56' should be followed by a 7 or end of string)
有没有一种特殊的方法来处理方括号中的 $ 字符?