sql - mySQL statement get last record -
i'm trying total of votes each fanof_id (ex: me). problem fan can vote each day same fanof_id (ex: david bowie rip)
so each day vote david bowie favorite singer
id created fan_id fanof_id 15 2016-01-24 3 3 16 2016-01-25 3 3 17 2016-01-25 2 3 so example should result of 2 fans 'total' fanof_id (3)
this actual sql
select distinct `fans_fanofvote`.`fan_id`, count(`fans_fanofvote`.`fanof_id`) `total` `fans_fanofvote` group `fans_fanofvote`.`fanof_id` order `total` desc but returns 3 records if use distinct on fan_id wont work. how can mysql distinct on fan_id
my sql should return 1 record that:
fanof_id total 3 2
you want count(distinct). however, have careful counting (fan_id) , aggregating (fanof_id):
select fov.fanof_id, count(distinct fov.fan_id) total fans_fanofvote fov group fov.fanof_id order total desc; note table aliases make query easier read. , don't use tick unless needed.
Comments
Post a Comment