在 MySQL
中,我想要查询信息模式以查找特定数据库中的表。
我在 select 语句中使用 Regexp
,如下所示。
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='testing' and TABLE_NAME REGEXP 'test|test_10'
输出:
123_test
123_test_10
temp_test
temp_test_10
这里我获取了以 test
和 test_10
结尾的表列表,如 select 语句中所示。
我在使用下面时得到了相同的结果。
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='testing' and TABLE_NAME REGEXP 'test'
输出:
123_test
123_test_10
temp_test
temp_test_10
使用第二条语句如何仅获取以“test”结尾的表的列表。
我想要的预期输出是
预期输出:
123_test
此外,我想排除列表中的一些表。就像排除以 temp
开头并以 test
结尾的表
最佳答案
如果您只需要表名,然后在测试中end,请不要使用regexp
。使用像
:
select TABLE_NAME
from information_schema.tables
where TABLE_SCHEMA = 'testing' and TABLE_NAME LIKE '%test' ;
为什么like
更可取?首先,它是标准的SQL。其次,在某些情况下(这不是其中之一)可以使用索引进行优化。
对于正则表达式,只需指定表达式需要位于名称末尾即可:
where TABLE_SCHEMA = 'testing' and TABLE_NAME REGEXP 'test$' ;
关于mysql - MySQL中select语句中使用Regexp的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44144873/