php - MySQL - How to use ROLLUP and ORDER BY in same query -
php - MySQL - How to use ROLLUP and ORDER BY in same query -
select donator, event, sum(amount) total_donation_per_donator tr_donation ngopkid=1 grouping donator, event rollup
using above query getting below result.
johnny cruz charity: event 001 | amount: us$ 100 charity: event 002 | amount: us$ 30 charity: event 003 | amount: us$ 90 total : us$ 220 mike polo charity: event 002 | amount: us$ 200 charity: event 004 | amount: us$ 125 total : us$ 325
now want sort info sub total of each donator (example: us$ 325, us$ 220) donator highest donation display first.
below query sorting records in ascending order if utilize desc 'total_donation_per_donator', it's sort info in descending error witch giving wrong result.
select * ( select *, sum(amount) total_donation_per_donator tr_donation ngopkid=1 grouping donator, event rollup ) temp_tr_donation order total_donation_per_donator
here table structure.
create table if not exists `tr_donation` ( `pkid` bigint(20) unsigned not null auto_increment, `ngopkid` bigint(20) unsigned not null default '0', `donator` varchar(100) not null default '', `eventpkid` bigint(20) unsigned not null default '0', `event` varchar(100) not null default '', `amount` float unsigned not null default '0', `postdate` datetime default '0000-00-00 00:00:00', primary key (`pkid`) ) engine=myisam default charset=latin1 auto_increment=1 ;
select donation.`donator`, donation.`event`, sum(donation.`amount`) total_donation_per_donator_and_event, sumdonation.total_donation_per_donator tr_donation donation inner bring together ( select `donator`, sum(`amount`) total_donation_per_donator tr_donation grouping `donator` ) sumdonation on sumdonation.`donator` = donation.`donator` grouping donation.`donator`, donation.`event` order sumdonation.total_donation_per_donator desc, donation.`event` asc
you should test query filled table before using in production environment. performance perchance poor..
php mysql
Comments
Post a Comment