How to find all the tables in MySQL with specific column names in them?

后端 未结 11 1469
天命终不由人
天命终不由人 2020-11-22 11:09

I have 2-3 different column names that I want to look up in the entire DB and list out all tables which have those columns. Any easy script?

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 11:37

    The problem with information_schema is that it can be terribly slow. It is faster to use the SHOW commands.

    After you select the database you first send the query SHOW TABLES. And then you do SHOW COLUMNS for each of the tables.

    In PHP that would look something like

    
        $res = mysqli_query("SHOW TABLES");
        while($row = mysqli_fetch_array($res))
        {   $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
            while($rw2 = mysqli_fetch_array($rs2))
            {   if($rw2[0] == $target)
                   ....
            }
        }
    
    

提交回复
热议问题