CREATE TABLE `p_account_log` (
`
log_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`
uid` mediumint(8) unsigned NOT NULL COMMENT '會員編號',
`
use_money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '可用資金',
`
change_time` int(10) unsigned NOT NULL COMMENT '變動時間',
`
change_desc` varchar(255) NOT NULL DEFAULT '' COMMENT '備注',
`
change_type` tinyint(3) unsigned NOT NULL COMMENT '變動類型0爲充值,1爲消費, 2爲充值會員 3爲推薦獎勵 99爲其他',
PRIMARY KEY (`log_id`),
KEY `user_id` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=82416 DEFAULT CHARSET=utf8 COMMENT='帳戶變動記錄表';
CREATE TABLE `p_order` (
`
order_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`
uid` int(11) DEFAULT '0' COMMENT '會員編號',
`
username` varchar(30) DEFAULT '' COMMENT '會員名',
`
courier_id` int(11) DEFAULT '0' COMMENT '快遞ID',
`
courier_name` varchar(180) DEFAULT '' COMMENT '快遞名稱',
`
sum_money` decimal(10,2) DEFAULT '0.00' COMMENT '付款金額',
`
order_status` tinyint(1) DEFAULT '0' COMMENT '訂單狀態',
`
pay_status` tinyint(1) DEFAULT '0' COMMENT '付款狀態',
`
add_time` int(10) DEFAULT '0' COMMENT '添加時間',
`
pay_time` int(10) DEFAULT '0' COMMENT '付款時間',
`
sender_name` varchar(30) DEFAULT '' COMMENT '發件人姓名',
`
sender_province` varchar(30) DEFAULT '' COMMENT '發件人省份',
`
sender_city` varchar(30) DEFAULT NULL COMMENT '發件人城市',
`
sender_district` varchar(30) DEFAULT '' COMMENT '發件人地區',
`
sender_address` varchar(180) DEFAULT '' COMMENT '發件人詳細地址',
`
sender_zip` varchar(10) DEFAULT '' COMMENT '郵政編碼',
`
sender_mobile` varchar(20) DEFAULT '' COMMENT '發件人手機',
`
sender_telphone` varchar(20) DEFAULT '' COMMENT '發件人座機',
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=63284 DEFAULT CHARSET=utf8 COMMENT='訂單表';
CREATE TABLE `p_user` (
`
uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`
username` varchar(30) DEFAULT '' COMMENT '用戶名',
`
password` varchar(32) DEFAULT '' COMMENT '密碼',
`
mobile` varchar(20) DEFAULT '' COMMENT '手機號',
`
qq` varchar(20) DEFAULT '' COMMENT 'QQ',
`
email` varchar(160) DEFAULT '' COMMENT '電子郵件',
`
amount` decimal(10,2) DEFAULT '0.00' COMMENT '金額',
`
agent_id` int(11) DEFAULT '0' COMMENT '會員等級ID',
`
start_time` int(10) DEFAULT '0' COMMENT '開始日期',
`
wenti` tinyint(2) DEFAULT '0' COMMENT '問題',
`
daan` varchar(100) DEFAULT '' COMMENT '密保答案',
`
referrer_uid` int(11) DEFAULT '0' COMMENT '推薦人',
`
status` tinyint(1) DEFAULT '1' COMMENT '0禁用 1啓用',
`
add_time` int(10) DEFAULT '0' COMMENT '注冊日期',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=4502 DEFAULT CHARSET=utf8 COMMENT='會員表';
SELECT t2.referrer_uid,t1.username,t2.count,
(SELECT sum(use_money) from p_account_log where change_type=3 and p_account_log.uid=t1.uid) as use_money,
SUM(t3.sum_money) as sum_money
from p_user t1
INNER JOIN (select referrer_uid,count(*) as count from p_user where referrer_uid>0 GROUP BY referrer_uid) t2
on t1.uid=t2.referrer_uid
INNER JOIN
(SELECT m1.uid,m2.referrer_uid,m2.username,sum(sum_money) as sum_money from p_order m1
INNER JOIN p_user m2 on m1.uid=m2.uid and m2.referrer_uid>0
GROUP BY m1.uid,m2.referrer_uid) t3
on t3.referrer_uid=t2.referrer_uid
group by t2.referrer_uid
ORDER BY use_money desc