Group output of SHOW COLUMNS into comma-delimited list Group output of SHOW COLUMNS into comma-delimited list mysql mysql

Group output of SHOW COLUMNS into comma-delimited list


Take a look at the information_schema.columns table

select group_concat(column_name order by ordinal_position)from information_schema.columnswhere table_schema = 'database_name' and table_name = 'table_name'

edit. Information schema allows you to make queries on metadata.So, you can even compare fields between tables with a left join for example.

edit. Hi Chris. Glad you've solved. As you said your problem was quite different because it concerned with different servers. I add an example of two different databases on the same server.

create database db1;use db1;create table table1(id int not null auto_increment primary key,name varchar(50),surname varchar(50),dob date)engine = myisam;create database db2;create table db2.table2 like db1.table1;alter table db2.table2 drop column dob;select i1.column_name from (select column_namefrom information_schema.columns where table_schema = 'db1' and table_name = 'table1' ) as i1left join (select column_namefrom information_schema.columns where table_schema = 'db2' and table_name = 'table2' ) as i2on i1.column_name = i2.column_namewhere i2.column_name is null

and the obvious result is dob that is present in table1 and not in table2.

Hope that it helps someone else. Regards guys. :)


You should use INFORMATION_SCHEMA.

You can copy the information_schema.columns table from each database into a shared schema and then run SQL queries to compare them.