|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem with SQL Distinct
Does anybody know why this isn't working:
SELECT DISTINCT(rowerid), age, weight, city, state, country, date_rowed, distance, time, comments FROM workouts WHERE distance='500' ORDER BY time ASC It displays everything, even if the rowerid is the same, which it shouldn't... |
|
#2
|
|||
|
|||
|
distinct doesn't work like that. its not a function. It will pull out unique rows, not based on a single column. everything in each row of the result must be unique or else it will not consider it distinct. If the workouts table has a auto_increment id column, and you want to pull out a list of the last workout from each unique rowerid, you can do:
Code:
SELECT rowerid, age, weight, city, state, country, date_rowed, distance, time, comments FROM workouts WHERE workouts_id IN (SELECT max(workouts_id) FROM workouts GROUP BY rowerid) the subquery gets a list of just the largest id number from workouts for each rowerid and then the main query will get the data for each id in that subquery. |
|
#3
|
|||
|
|||
|
Thanks for your help. It doesn't quite work as it is for a leaderboard, and the row-id does not indicated placement. I played with it and tried changing column names, but it still doesn't work.
Unless you have a better suggestion, I will just add a column at the end of the table indicating if it is the "ranked" one or not and check every time they insert a time and update it. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > Problem with SQL Distinct |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|