Reputation: 25
I am trying to get the sum hours for each month from a given user and having problems with the code. Need help.
what I have so far:
$sql = "SELECT sum(hours) AS total_Hours FROM flights WHERE month = 3 and username='$myname'";
This isn't even working, But what i really need is total hours from user for each month. possible?
Upvotes: 0
Views: 169
Reputation: 40512
Here is the query asking sum for each month:
SELECT SUM(`hours`) AS total_Hours, `month` FROM flights
WHERE `username`='some_user'
GROUP BY `month`
Also you should know that your $myname
variable must to be escaped with mysql_real_escape_string before passing in a query.
@Robbie is telling right about escaping month
. It seems to be the only problem of your query.
Upvotes: 1
Reputation: 360842
You need a GROUP BY
clause:
SELECT user, month, SUM(hours) AS total_hours
FROM flights
GROUP BY user, month
ORDER BY user, month
Upvotes: 0