MySQL loop through tables

后端 未结 4 984
花落未央
花落未央 2020-12-06 07:11

I want to output data from every table that contains a first_name column. I put together the following procedure, but in my loop, mysql interprets the table nam

4条回答
  •  Happy的楠姐
    2020-12-06 07:24

    a little edit of the above to itertate ahtoug all the tables and select them.

    delimiter //
    drop procedure if exists hunt //
    create procedure hunt()
    begin
        DECLARE done int default false;
        DECLARE table_name CHAR(255);
    
        DECLARE cur1 cursor for SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
                      WHERE table_schema ='mbu4u';
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
        open cur1;
           myloop: loop
            fetch cur1 into table_name;
            if done then
                leave myloop;
            end if;
            set @sql = CONCAT('select * from `mbu4u`.',table_name);
            prepare stmt from @sql;
            execute stmt;
            drop prepare stmt;
        end loop;
    
        close cur1;
    end //
    
    delimiter //
    

提交回复
热议问题