Jump to content

SQL - problem selecting distinct data


BeanoEFC

Recommended Posts

Hey Everyone,

 

I am trying to display some data i have in my mysql database in a table.  im having a problem with the SQL to do it though.  what i am trying to do is display the amount of calories for a day, there could be multiple entries for a day.

 

This is the data from my database:

+-----------------------------+-----------------+
| substring(r.ride_date,1,10) | calories_burned |
+-----------------------------+-----------------+
| 2010-01-08                  |             250 | 
| 2010-01-08                  |             123 | 
| 2010-01-08                  |             123 | 
| 2010-01-10                  |             456 | 
+-----------------------------+-----------------+

What i want is to select the the distinct days (2010-01-08 and 2010-01-10) and add up the calories for them.

 

the SQL i am using is this:

select distinct(substring(r.ride_date,1,10)), sum(calories_burned)
from rides r 
join usersrides ur on r.ride_date = ur.ride_date 
join users u on ur.userid = u.userid 
where u.userid='1';

this does not produce the result im looking for though, it just sums up all the calories burned into one row

+-------------------------------+----------------------+
| (substring(r.ride_date,1,10)) | sum(calories_burned) |
+-------------------------------+----------------------+
| 2010-01-08                    |                  952 | 
+-------------------------------+----------------------+

 

Please can someone point me in the rite direction

Thanks in Advance,

-Beano

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/188040-sql-problem-selecting-distinct-data/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.