zoukankan      html  css  js  c++  java
  • Mysql查询特定值是哪些表哪些字段

    摘自网上 

    -- 查询整个数据库中某个特定值所在的表和字段的方法
    # flush tables;
    
    -- 创建表来存储查询结果
    
    drop table if exists tmp_table;
    
    
    CREATE TABLE tmp_table (
    
      tablename   VARCHAR(1024) null,
    
      columnname  VARCHAR(1024) null,
    
      columnvalue VARCHAR(1024) null
    
    );
    
    
    DROP PROCEDURE IF EXISTS search_value;
    
    DELIMITER $$
    -- v1内容值
    CREATE PROCEDURE search_value(v1 VARCHAR(1024))
      BEGIN
    
        DECLARE done INT DEFAULT 0;
        DECLARE m_table VARCHAR(64);
        DECLARE m_column VARCHAR(64);
    
        -- 查询数据库字段类型为'varchar' 的字段
        DECLARE m_tables CURSOR
        FOR
          select table_name, column_name
          from information_schema.columns
          where data_type = 'varchar'
          -- 注意修改这里的 table_schema
          and table_schema = 'table_schema'
        --      and table_name = 'biz_patient_register'
        ;
        declare continue handler for not FOUND set done = 1;
    
        set @_v = v1;
        open m_tables;
        FETCH m_tables
        INTO m_table, m_column;
        WHILE done != 1 do
          #       insert into tmp_table select m_table as tablename, m_column as columnname, v1 as columnvalue;
          set @m_sql = concat('insert into tmp_table select ''', m_table, ''' as tablename,''', m_column,
                              ''' as columnname,`', m_column, '` as columnvalue from `', m_table, '` where `', m_column,
                              '` = ''%', v1, '%'';');
          -- 编译sql
          prepare stmt from @m_sql;
    
          -- 执行sqL
          EXECUTE stmt;
          deallocate prepare stmt;
          #     select m_table, m_column;
          FETCH m_tables
          INTO m_table, m_column;
        END WHILE;
    
        CLOSE m_tables;
      End $$
    DELIMITER ;
    
    -- 存储过程创建完成
    call search_value('152'); -- 执行存储过程
    select *
    from tmp_table; -- 查询存储过程执行的结果
    

      

  • 相关阅读:
    Windows 10安装Docker并使用私钥连接AWS EC2
    Logback中%X的使用
    使用CompletableFuture+ExecutorService+Logback的多线程测试
    Spring Boot与Spring Session集成
    Java 8中Collection转为Map的方法
    记一次OutOfMemory定位过程-续
    记一次OutOfMemory定位过程
    Jmeter学习之While Controller
    使用VirtualBox虚拟机搭建局域网(续)
    Java 8的Lambda学习
  • 原文地址:https://www.cnblogs.com/wuyifu/p/10237547.html
Copyright © 2011-2022 走看看