0

I am querying a database as:

SELECT SUM(Hours) 
  from User 
 where surname = 'johnson';

Whenever I run it, as you know it will show selected Column with the name SUM(Hours).

Question:

Is there any way that I can "rename" it without altering the database table?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
7dr3am7
  • 755
  • 1
  • 8
  • 21

2 Answers2

5

You have to do something like this:

SELECT SUM(Hours) AS sumHours FROM User WHERE surname='johnson';

Now your column name is sumHours. You can name it whatever you want, but keep in mind to be descriptive.

Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50
  • You just have to add `AS whatColumnNameYouWant` after `SUM(Hours)` and after that you will see the result as `whatColumnNameYouWant`. It works. Used it million times :) – Bogdan Constantinescu Mar 09 '11 at 23:12
  • 1
    @7dr3am7: The `AS` keyword is a supported feature for specifying a column alias in MySQL since 4x IME. Your issue, whatever it is, is then unlikely to be the column alias. Provide more detail than "it doesn't work" if expect anyone to answer you. It's possible you're trying to specify an alias that is a registered keyword, which would require enclosing in backticks to work... or you could just not use registered keywords in your queries as column names. – OMG Ponies Mar 09 '11 at 23:14
  • You won't believe it guys but.... O.O 48 hours of non sleep can make you put semicolons around a very simple SQL STATEMENT! O.O THANK you all.. I think I will go to sleep now....thank you all for your patient – 7dr3am7 Mar 09 '11 at 23:17
1
SELECT SUM(Hours) as hours from User where surname='johnson';
nickmoriarty
  • 1,253
  • 19
  • 21
  • @Johan You do realize we both answered this at almost the same time, so I wouldn't have seen the other answer until after mine was posted. You might want to look closer before you downvote just to get your badge. – nickmoriarty Apr 07 '11 at 17:44
  • Sorry 'bout that did not mean to earn my batch off of your karma, will up vote somewhere to compensate. – Johan Apr 07 '11 at 23:34