我想按以下方式实现计数系统
test_id email_id attemptCount status score subject
1 a@a1.com 1 Fail 5 C
1 a@a1.com 1 Pass 72 maths
1 b@a1.com 1 Pass 62 C请看,(test_id,email_id, attempt_count, subject)形成了唯一的行。
我想为每次测试尝试插入一行。因此,对于attemptCount,same user, same test id, same subject必须增加一个。
我能做什么?
1)我可以从另一个表中获得键的最后尝试计数(测试id、电子邮件id、主题)。
test_id email_id subject last_attempt 2)可以使用触发器自动递增尝试计数。我只是在其中一篇文章中读到了这一条
3)我必须改变我的桌子结构。因此,我将为我的每一个主题单独的桌子。所以主键组合将是test_id, email_id, attempt_count
还有其他方法可以做到这一点吗?哪一种是最好的方法?
我觉得我得重新考虑一下我的桌子结构了。
编辑
在单个单元格中具有以下多个值是一个良好的实践吗?
test_id email_id overallStatus overallScore subject
1 a@a1.com [pass,fail,fail,pass] [10,2,3,10] maths然后,组合(test_id, email_id, subject)只有一行。在这种情况下,我不关心attemptCount。
我的目标是为所有用户的所有测试存储所有的尝试结果。
发布于 2016-02-08 15:42:56
我个人会用三张桌子。
测试、主题和test_result
我有这样的测试
test_id
email_id
//other columns such as date, description, etc表主题
subject_id (auto_increment)
test_id
attempts
//you can add date and the latest status or score这就像一个历史表,以防您想要一份报告:
test_result
test_result_id(auto increment)
test_id
subject
status
score因此,当您在test_result上插入时,您可以在尝试时使用+1更新主题(从表主题),如果您想更新该表的最新结果(为了避免对这个表进行很长的查询,您也可以这样做)
发布于 2016-02-08 15:26:14
您的方法/函数必须在某种编程方法中创建,您将无法实现所有这一切,只将数据传递给数据库。
在您的auto_increment上使用test_id是正确的,但是您的主键是您的test_id。
这没什么不好的。
https://stackoverflow.com/questions/35273129
复制相似问题