Select From all tables - MySQL

前端 未结 6 429
灰色年华
灰色年华 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:36

    Improve to @inno answer

    delimiter //
    
    DROP PROCEDURE IF EXISTS get_product;  
    CREATE PROCEDURE get_product()
    BEGIN
       DECLARE i VARCHAR(100); 
       DECLARE cur1 CURSOR FOR SELECT DISTINCT table_name FROM information_schema.columns WHERE COLUMN_NAME IN ('Product');
       OPEN cur1;
    
       read_loop: LOOP
           FETCH cur1 INTO i;
        
           SELECT i; -- printing table name
        
           SET @s = CONCAT('select * from ', i, ' where Product like %XYZ%'); 
           PREPARE stmt1 FROM @s;
           EXECUTE stmt1;
           DEALLOCATE PREPARE stmt1;
    
       END LOOP read_loop;
    
       CLOSE cur1;
    END//
    
    delimiter ;
    
    call get_product();
    

提交回复
热议问题