sql >> Base de Datos >  >> RDS >> Mysql

MySQL mínimo y máximo de cada columna

Necesita usar sql dinámico:

set @sql = 'SELECT @cols FROM @t';

SELECT @sql := GROUP_CONCAT(REPLACE(REPLACE(@sql, @cols,
                                            CONCAT(COLUMN_NAME, ', ', DATA_TYPE, ', ',
                                                   'MIN(', COLUMN_NAME, '), MAX(', COLUMN_NAME, ')'
                                                  )
                                           ),
                                    @t, 'tbl_name') SEPARATOR ' union all '
                            )              
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tbl_name' AND DATA_TYPE = 'decimal';

prepare s from @sql;
execute s;
deallocate prepare s;