选择具有特定模式的表名

问题描述 投票:0回答:2

我创建了一个查询来获取"prefix_"的所有表名

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE 'prefix_%' AND TABLE_TYPE = 'BASE TABLE'

但是在我的数据库中,我有带prefix_prefix_的表。如何避免选择它们?

sql
2个回答
2
投票

您只需将例外添加到where子句即可。

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE 'prefix_%' AND TABLE_NAME NOT LIKE 'prefix_prefix_%' AND TABLE_TYPE = 'BASE TABLE'

2
投票

您可以尝试使用子查询而不是

   select * from 
   (
     SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 
     WHERE TABLE_NAME LIKE 'prefix_%' and AND TABLE_TYPE = 'BASE TABLE'
   )A where ABLE_NAME LIKE 'prefix_prefix_%'
© www.soinside.com 2019 - 2024. All rights reserved.