merge values in mysql and sort
I a mysql table like 开发者_开发百科this:
CREATE TABLE vote (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(16) NOT NULL,
`site` varchar(100) NOT NULL,
`nr` bigint(20) NOT NULL default '1',
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
The same user can have many entries. I want to sort by highest nr
, but since the same username
can have many entries with different nr
. Same username
entries must merge and the nr
values must be added together.
I think you are looking for something along the lines of:
select username, sum(nr) as nrSum
from vote
group by username
order by nrSum desc
精彩评论