开发者

Lower case column names when using SELECT

I'm using the Kohana framework and I need to convert column names to lowercase. I don't have control of the db table structure. I want to do the following: SELECT LOWER(*) FROM .....

but MYSQL does not like that. Whats the proper way of outp开发者_StackOverflowutting the lower case column names if I don't know what the column names will be?


Found here http://dev.mysql.com/doc/refman/5.0/en/columns-table.html

SELECT LOWER(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Table'

Below you can see both MSSQL and MySQL syntax for creating a dynamic query using the column results from the query above.

MSSQL Syntax


DECLARE @ColumnNames [nvarchar](1024)

SELECT @ColumnNames = COALESCE(@ColumnNames + ', ', '') + LOWER(COLUMN_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table'

DECLARE @Sql [nvarchar](1024) = 'SELECT ' + @ColumnNames + ' FROM Table ' --Remember to put spaces after SELECT and before FROM

EXEC(@Sql)

With this you are dynamically building your query and then executing it.

MySQL Syntax


SELECT @ColumnNames := GROUP_CONCAT(LOWER(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table';

SET @Sql = CONCAT('SELECT ', @ColumnNames, ' FROM Table ');

PREPARE stmt1 FROM @Sql;
EXECUTE stmt1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜