Re: mySQL query help
Quote:
|
Originally Posted by REY619
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 table_name 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
table_name
WHERE
visitorid='1'
GROUP BY
name
ORDER BY
MAX_TIME DESC;
__________________
Desktop: P4 2.8E, Intel 865GBF, 512MB Hynix PC3200, Samsung SV1204H 120GB, Samsung SW-248F, Creative Inspire 4400, Samsung 793MB, Compro PVR/FM, WinLIRC
Laptop: HP Pavilion dv8210us
|