mysql - select columns dynamically based on list of column names obtained from -
i need simple select statement, based on list of column names dynamic , filtered lower case column names in table. table structure out of control , varies. not possible me know column names before hand - there upper case names (not wanted) , lower case names (wanted).
the_table:
col_uppercase_1 col_uppercase_2 col_lowercase_1 col_lowercase_2 data1 data2 data3 data4 data5 data6 data7 data8
i can list of column names want using this:
select group_concat(column_name) `information_schema`.`columns` (`table_schema` = 'the_database' , `table_name` = 'the_table' , column_name = binary lower(column_name));
which returns list of columns want:
+---------------------------------+ | group_concat(column_name) | +---------------------------------+ | col_lowercase_1,col_lowercase_2 | +---------------------------------+
my question: how insert results of query select statement? e.g.
select <column list above "select group_concat..." query> my_table;
specifically, in example above, want equivalent of:
select col_lowercase_1,col_lowercase_2 the_table;
to return:
+---------------------------------+ | col_lowercase_1 col_lowercase_2 | +---------------------------------+ | data3 data4 | | data7 data8 | +---------------------------------+
you can use exec()
run query text
e.g.
declare @columns varchar(8000); select @columns = select group_concat(column_name) `information_schema`.`columns` (`table_schema` = 'the_database' , `table_name` = 'the_table' , column_name = binary lower(column_name)); exec ( 'select ' + @columns ' the_table' );
Comments
Post a Comment