Bug #34723 Wrong column name reporting
Submitted: 21 Feb 2008 12:09 Modified: 21 Feb 2008 12:22
Reporter: Roberto Marrodán Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.5 OS:Windows
Assigned to: CPU Architecture:Any
Tags: column name originalColumnName

[21 Feb 2008 12:09] Roberto Marrodán
Description:
When you query a column of a table and you alias it (select id as userIdentifier ...) MySQL JDBC driver returns the original column name (id) instead of the aliased one (userIdentifier). If the field is a derived one (e.g.: concat(id)) then it reports the alias fine.
This bug didn't take place in 5.0.8 but does on 5.1.5.
It seems that the original column name is taking priority instead of the aliased one.

How to repeat:
Given a table T1 with a field F1, execute:
 select F1 as alias1 from T1;
then retrieve the resultset, then its metadata and call ResultSetMetadata.getColumnName(1). This will return "F1" instead of "alias1".

Suggested fix:
For the example query stated above:
 select F1 as alias1 from T1;
substitute it for the following:
 select concat(F1) as alias1 from T1;
this converts the field into a derived one and therefore the aliased name will be picked up.
[21 Feb 2008 12:22] Tonci Grgin
Please do not submit the same bug more than once. An existing bug report already describes this very problem. Even if you feel that your issue is somewhat different, the resolution is likely to be the same. Because of this, we hope you add your comments to the original bug instead.

Thank you for your interest in MySQL.

Explanation: Hi Roberto and thanks for your report. This is a duplicate of Bug#32504 and Bug#31499 and many others.

Changelog:
    - Return original column name for RSMD.getColumnName() if the column was aliased, alias name for .getColumnLabel() (if aliased), and original table name for .getTableName(). Note this only works for MySQL-4.1 and newer, as older servers don't make this information available to clients.

and docs:
useOldAliasMetadataBehavior: Should the driver use the legacy behavior for "AS" clauses on columns and tables, and only return aliases (if any) for Result- SetMetaData.getColumnName() or ResultSet-MetaData.getTableName() rather than the original column/table name?
false 5.0.4

Please see Mark's explanation in Bug#31499.