database - MySQL concatenating all columns -


why can not concatenate in mysql using * keyword?

select concat(*) table 

or

select group_concat(*) table 

is there other way access values in column without explicitly using columns name?

to concatenate columns in table, can't use * keyword, need explicitly list columns:

select concat(col1, col2, col3, ....) yourtable 

or might want use concat_ws skip null values:

select concat_ws(',', col1, col2, col3, ....) yourtable 

if don't want specify column names manually, use dinamic query. query return column names of table:

select `column_name`    `information_schema`.`columns`   `table_schema`=database()         , `table_name`='yourtable'; 

and using group_concat can obtain list of column names:

group_concat(concat('`', column_name, '`')) 

quoted, in comma separated format:

`col1`,`col2`,`col3`,`col4`,... 

so have elements create our query dinamically:

select   concat(     'select concat_ws(\'\',',     group_concat(concat('`', column_name, '`') order column_name),     ') all_columns yourtable;')   `information_schema`.`columns`   `table_schema`=database()         , `table_name`='yourtable' @sql; 

this query set @sql string like:

select concat_ws('', col1, col2, col3, ....) all_columns yourtable 

and code execute it:

prepare stmt @sql; execute stmt; 

please see fiddle here.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -