Select From all tables - MySQL

前端 未结 6 425
灰色年华
灰色年华 2020-12-10 10:54

I have a mySQL database called listDB that contain several tables with column name Product etc. I want to SELECT from all tables where

6条回答
  •  死守一世寂寞
    2020-12-10 11:41

    You can get all tables that has column "Product" from information_Schema.columns

    SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name ="Product";
    

    Nor create a procedure

    delimiter //
    CREATE PROCEDURE curdemo()
    BEGIN
      DECLARE a varchar(100); 
      DECLARE cur1 CURSOR FOR SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name ="Product";
      OPEN cur1;
    
      read_loop: LOOP
        FETCH cur1 INTO a;
    
        SELECT * FROM a;
    
      END LOOP;
    
      CLOSE cur1;
    END;
    
    delimiter ;
    
    call curdemo();
    

提交回复
热议问题