Obtener todas las columnas de todas las tablas MySQL


¿Hay una forma rápida de obtener todos los nombres de columna de todas las tablas en MySQL, sin tener que enumerar todas las tablas?

 146
Author: Ben, 2011-04-13

10 answers

select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
 238
Author: Nicola Cossu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-13 11:50:32

Para listar todos los campos de una tabla en MySQL:

select * 
  from information_schema.columns 
 where table_schema = 'your_DB_name' 
   and table_name = 'Your_tablename'
 37
Author: suganya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-16 16:25:20
SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position

Dado que no tengo suficiente rep para comentar, aquí hay una mejora menor (en mi opinión) sobre la excelente respuesta de nick rulez: reemplazar WHERE table_schema = 'your_db' por WHERE table_schema = DATABASE().

 24
Author: appel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-10 13:56:46

Es mejor que utilice la siguiente consulta para obtener todos los nombres de columna fácilmente

Show columns from tablename

 17
Author: rajesh satpute,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-08 13:40:13

En el caso de que sea útil para cualquier otra persona, esto le dará una lista delimitada por comas de las columnas en cada tabla:

SELECT table_name,GROUP_CONCAT(column_name ORDER BY ordinal_position)
FROM information_schema.columns
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name

Nota: Al usar tablas con un número alto de columnas y/o con nombres de campo largos, tenga en cuenta el límite group_concat_max_len, que puede hacer que los datos se trunquen.

 15
Author: trapper_hag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-13 16:05:27
<?php
        $table = 'orders';
        $query = "SHOW COLUMNS FROM $table";
        if($output = mysql_query($query)):
            $columns = array();
            while($result = mysql_fetch_assoc($output)):
                $columns[] = $result['Field'];
            endwhile;
        endif;
        echo '<pre>';
        print_r($columns);
        echo '</pre>';
?>
 5
Author: Ali Nawaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-29 11:10:23

Similar a la respuesta publicada por @suganya esto no responde directamente a la pregunta, pero es una alternativa más rápida para una sola tabla:

DESCRIBE column_name;
 3
Author: Philip Kirkbride,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:50

Escribí esta tontería hace mucho tiempo y todavía la uso de vez en cuando:

Https://gist.github.com/kphretiq/e2f924416a326895233d

Básicamente, hace un "MOSTRAR TABLAS", luego un "DESCRIBIR" en cada tabla, luego lo escupe como markdown.

Simplemente edite debajo del "if name" y vaya. Usted necesitará tener pymysql instalado.

 2
Author: lysdexia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-30 20:32:27

Aprovechando la respuesta de Nicola con un php legible

$a = mysqli_query($conn,"select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position");
$b = mysqli_fetch_all($a,MYSQLI_ASSOC);
$d = array();
foreach($b as $c){
    if(!is_array($d[$c['TABLE_NAME']])){
        $d[$c['TABLE_NAME']] = array();
    }
    $d[$c['TABLE_NAME']][] = $c['COLUMN_NAME'];
}
echo "<pre>",print_r($d),"</pre>";
 2
Author: Scot Nery,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-14 02:14:25

La pregunta era:

¿Hay una forma rápida de obtener todos los NOMBRES de COLUMNA de todas las tablas en MySQL, sin tener que enumerar todas las tablas?

SQL para obtener toda la información de cada columna

select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

SQL para obtener todos los NOMBRES DE COLUMNA

select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
 1
Author: J.BizMai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-26 13:33:31