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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
@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.
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
1
SELECT SUM(Hours) as hours from User where surname='johnson';

2 Comments

@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.
Sorry 'bout that did not mean to earn my batch off of your karma, will up vote somewhere to compensate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.