【问题标题】:Mysql regexp to get right table names from database schema [duplicate]Mysql regexp从数据库模式中获取正确的表名[重复]
【发布时间】:2021-02-03 22:53:44
【问题描述】:

我正在尝试从 mysql 中选择一些与模式匹配的特定表。

我正在寻找的特定模式类似于“my_table_number”。例如,my_table_436814my_table_35413。问题是还有其他表格看起来像my_table_old_14353my_table_351434_times。我只想过滤掉my_table_number

我尝试了不同的模式,但没有得到我真正需要的。

最接近的方法是:

select table_name from 
information_schema.columns ca 
where ca.table_name REGEXP '[0-9]$'

我怎样才能实现我正在寻找的东西?

【问题讨论】:

  • my_table 是固定字符串吗?
  • @WiktorStribiżew 是的
  • 好的,那么下面的答案就是做你需要的。

标签: mysql regex


【解决方案1】:

使用

REGEXP '^my_table_[0-9]+$'

proof

NODE EXPLANATION
^ the beginning of the string
my_table_ 'my_table_'
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
$ before an optional \n, and the end of the string

【讨论】:

  • 这实际上解决了问题。非常感谢你!最后一件事,无论有没有^ 符号,这都适用。为什么?
  • @xerac 如果您不想确保匹配恰好在字符串的开头,则不需要^