【问题标题】:How find an value in to all table postgress如何在所有表 postgres 中找到一个值
【发布时间】:2021-09-05 08:19:05
【问题描述】:

假设有这个词 'student' ,我需要返回所有包含 'student' 词的表。我需要这样的东西:

select *
from information_schema.tables t 
where column ='student';

谁能帮帮我?

【问题讨论】:

  • 请展示一些样本数据和该样本数据的预期结果;目前尚不清楚您是要搜索表定义还是表数据或两者都搜索
  • SQL 中的S 代表结构化......您想以非结构化方式查找内容吗?

标签: mysql sql postgresql


【解决方案1】:

如果你想要表格,你会使用:

select *
from information_schema.tables t 
where table_name like '%student%';

如果您想要,您将使用正确的元数据表并使用:

select *
from information_schema.columns c
where column_name like '%student%';

【讨论】:

  • 我的问题是没有“列名”,但我需要检查所有表和列中是否有值“学生”
【解决方案2】:

所以您想在数据库中查找字符串“students”或其任何版本。好吧,没有简单的查询可以为您提供。因此,您需要从具有字符串类型定义的信息架构 所有列名称 中进行选择,并为每一列构建查询。如果您只需要标准列类型,那么执行此操作的例程不会过于复杂或庞大。没有用户定义的类型,没有 JSON(B)、XML 或 hstore 类型。你可以试试:

do $$
declare 
    k_sql_base constant text = $stmt$ 
                                 select '%1$s' table_schema,'%2$s' table_name,'%3$s' column_name
                                   from %1$I.%2$I  where lower(%3$I) like '%4$s' limit 1;
                               $stmt$;  
    k_search_text text = '%students%'; 
                                           
    c_text_cols cursor for 
        select col.table_schema
             , col.table_name
             , col.column_name 
          from information_schema.columns col
         where col.data_type in ('text', 'character varying')
           and col.table_schema not in ('pg_catalog', 'information_schema');
    
    l_table_rec   record;      
    l_search_stmt text; 

begin 
   for l_table_rec in c_text_cols
   loop
       l_search_stmt = format(k_sql_base
                             ,l_table_rec.table_schema
                             ,l_table_rec.table_name
                             ,l_table_rec.column_name
                             ,k_search_text
                             ) ;
       execute l_search_stmt into l_table_rec;
       if l_table_rec.table_schema is not null then  
           raise notice 'Found in: %.%.%',l_table_rec.table_schema
                                         ,l_table_rec.table_name
                                         ,l_table_rec.column_name;       
       end if;
    end loop;
end;
$$; 


       

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 2011-02-05
    • 2021-10-11
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2014-01-15
    相关资源
    最近更新 更多