mySQL query help

Status
Not open for further replies.

REY619

Ambassador of Buzz
i have a table with fields name, time, and userid. I want to select name and time, such that no name is duplicated i.e. no same names should be returned and the result should be sorted by time(DESC). I cant make a query for this. Can someone help.
I tried this query but it doesnt work::
Code:
SELECT DISTINCT(name), time FROM [i]table_name[/i] WHERE visitorid='1' ORDER BY time DESC;
but it gives error.
Thanx.
 

mod-the-pc

Back to School Mr. Bean !
REY619 said:
i have a table with fields name, time, and userid. I want to select name and time, such that no name is duplicated i.e. no same names should be returned and the result should be sorted by time(DESC). I cant make a query for this. Can someone help.
I tried this query but it doesnt work::
Code:
SELECT DISTINCT(name), time FROM [I]table_name[/I] WHERE visitorid='1' ORDER BY time DESC;
but it gives error.
Thanx.
@REY619 a DISTINCT clause selects the distinct combinations of the fields in the SELECT clause (i.e. name & time in your query) and NOT just the distinct values of a single field. I guess that your table has more than one time associated with a name i.e. more than one record for the same name. You can just choose the max (or min) of time to ensure that the name is not repeated

Code:
SELECT 
  name, 
  max(time) as MAX_TIME 
FROM 
  [I]table_name[/I] 
WHERE 
  visitorid='1' 
GROUP BY 
  name 
ORDER BY 
  MAX_TIME DESC;
 
OP
R

REY619

Ambassador of Buzz
Thanx mod-the-pc will try that :)
__________
Thanx its working!!! :)
 
Last edited:
Status
Not open for further replies.
Top Bottom