Wednesday, December 18, 2013

MySQL: Column alias on negative value selection

Whenever you select a negative value from MySQL table, "-" will become a part of the field name returned in a result set. 

For example, we have following statement:

select id from MY_TABLE;
It will return a result with field name "id".

select -id from MY_TABLE;
will return a result with field name "-id".

select -custom.id from MY_TABLE custom;
will also include table alias name and return a result with field name "-custom.id".
In such case you should use "as %FIELD_NAME%" to be sure that you will have same field name:
select -custom.id as id from MY_TABLE custom;