我有一个基于MySQL/PHP的奖励系统。教师奖励学生积分;学生可以使用积分积分购买奖励。购买奖励的学生的百分比
我想要做的是显示已购买奖励的学生的单个百分比数字。
这听起来很简单,但实际上可能会稍微复杂一点。
- 我没有生在数据库中的表,我只是有
transactions
- 如果学生已经收到了点,他们的
ID
的列下会显示在transactions
表
的transactions
表如下所示:Transaction_ID
,Datetime
,Giver_ID
,,Points
,Category_ID
,Reason
。
的purchases
表如下所示:Purchase_ID
,Datetime
,Reward_ID
,Quantity
,Student_ID
,Student_Name
(空白),Date_DealtWith
,Date_Collected
因此,例如,我可以列出所有使用SELECT DISTINCT Recipient_ID FROM transactions
我的学生证的。
所有基本上,我需要的是:
- (学生)的学生用点+(即
transactions
)计数 - [采购]学生在'购买+(即
Student_ID
计数购买) - [采购]/[学生] * 100
..但我不知道怎么做,在一个查询!
编辑:INSERT语句*
购买...
INSERT INTO `purchases` (`Purchase_ID`, `Datetime`, `Reward_ID`, `Quantity`, `Student_ID`, `Student_Name`, `Date_DealtWith`, `Date_Collected`) VALUES
(1, '2011-09-27 16:55:16', 1, 1, 34240, '', '2011-09-27 16:55:16', '2011-12-12 15:45:43'),
(2, '2011-09-28 13:02:26', 1, 1, 137636, '', '2011-09-27 16:55:16', '2011-09-27 16:55:16'),
(3, '2011-09-29 11:29:09', 1, 1, 137685, '', NULL, NULL);
交易...
INSERT INTO `transactions` (`Transaction_ID`, `Datetime`, `Giver_ID`, `Recipient_ID`, `Points`, `Category_ID`, `Reason`) VALUES
(1, '2011-09-07', 36754, 34401, 5, 6, 'Gave excellent feedback on the new student notebook'),
(2, '2011-09-07', 34972, 137615, 10, 9, 'Helping TG'),
(6, '2011-09-07', 35006, 90185, 2, 1, '');
我觉得您的问题的关键是“GROUP BY”和“COUNT”功能。如果你发布了创建语句,以及一些插入语句的伪数据,我可以编写查询来选择你想要的内容 – 2012-03-13 16:33:41
Hi @BillyMoon,谢谢 - 我已经更新了我的OP。 – dunc 2012-03-13 16:40:29
看起来像你得到了你的答案 - 在这里它是一个要点:https://gist.github.com/2029991(自包含的测试代码可以粘贴到mysql控制台) – 2012-03-13 17:18:16