【问题标题】:SQL Query to find all table names on a database with MySQL使用 MySQL 查找数据库中所有表名的 SQL 查询
【发布时间】:2021-02-17 18:39:25
【问题描述】:

这些是数据库 MySql 版本 8.0.17 上的表列表

t_contents_s300_10_2021
t_contents_s300_1_2021
t_contents_s300_2_2021
t_contents_s300_3_2021
t_contents_s34d_1_2021
t_contents_s34g_1_2021
t_contents_s34g_2_2021
t_contents_s3sv_1_2021
t_contents_s3sv_2_2021

我需要在这个表格列表中找到所有表格,例如 s312021

我已经尝试过这个查询,但返回包含所有数字,而不仅仅是1

如何解决?

提前感谢您的帮助。

mysql> SELECT
    table_name 
FROM
    information_schema.TABLES 
WHERE
    table_name LIKE ( 't_contents_s3%' );
+-------------------------+
| TABLE_NAME              |
+-------------------------+
| t_contents_s300_10_2021 |
| t_contents_s300_1_2021  |
| t_contents_s300_2_2021  |
| t_contents_s300_3_2021  |
| t_contents_s34d_1_2021  |
| t_contents_s34g_1_2021  |
| t_contents_s34g_2_2021  |
| t_contents_s3sv_1_2021  |
| t_contents_s3sv_2_2021  |
+-------------------------+
9 rows in set (0.44 sec)

【问题讨论】:

    标签: mysql find tablename


    【解决方案1】:

    LIKE是个好方向:

    SELECT  table_name 
    FROM  information_schema.TABLES 
    WHERE  table_name LIKE  't#_contents#_s3%#_1#_2021' ESCAPE '#';
    

    或者如果s3%部分总是有4个字符,那么:

    SELECT  table_name 
    FROM  information_schema.TABLES 
    WHERE  table_name LIKE  't#_contents#_s3__#_1#_2021' ESCAPE '#';
    

    ESPACE 允许将 _ 视为 _ 而不是单个字符通配符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2023-03-10
      • 2011-02-13
      相关资源
      最近更新 更多