MySQL: How to fetch all columns with distinct clause on one column with latest created record -
i have db table structure
id, latitude, longitude, altitude, deviceid, createddate
i trying create query fetches distinct deviceid has been created @ latest..
i have tried query
select distinct deviceid `deviceposition` order createddate desc
based on beiller's post, try solution:
select deviceid, createddate `deviceposition` outer_dev_pos createddate = ( select max(inner_dev_pos.createddate) `deviceposition` inner_dev_pos inner_dev_pos.deviceid = outer_dev_pos.deviceid ) order createddate desc;
update: explanation:
we accessing same table 2 times in query, once in outer select , once in subselect (the inner select). therefor, necessary tell in query access' deviceid , createddata mean. important in clause of subselect:
where inner_dev_pos.deviceid = outer_dev_pos.deviceid
if didn't use alias names inner_dev_pos , outer_dev_pos 2 accesses of same table, line read:
where deviceid = deviceid
which not make sense.
Comments
Post a Comment