MySQL DB用于考试(测试测验)查询点数

时间:2017-04-10 05:33:02

标签: mysql sql

我有一个数据库,我存储了一个包含用户信息的表格,一个包含测试的表格(答案和点数)以及一个包含用户每个问题答案的表格。每个问题总值1分,可能有一个或多个正确的答案。如果所有答案都是正确的,并且用户只检查一个,那么他只会得到0.25分。 我想查询每个用户的总积分,但我找不到好的方法。

用户表:

+--------+------------+-----------+-------------------+------------+--------+
| userID | first_name | last_name | email             | password   | points |
+--------+------------+-----------+-------------------+------------+--------+
|      1 | Jhon       | Jhonny    | jhon@yahoo.com    | secretPass |      0 |
|      2 | Dan        | Dan       | dan@yahoo.com     | 1234       |      0 |
|      3 | Dick       | Pop       | dd@yahoo.com      | 123456     |      0 |
|      4 | Mihaela    | Micky     | mihaela@yahoo.com | pass12     |      0 |
+--------+------------+-----------+-------------------+------------+--------+

问题表: (1表示答案很好 - 我们可以有多个正确答案)

+------------+--------------------------------------------------+---+---+---+---+
| questionID | question                                         | a | b | c | d |
+------------+--------------------------------------------------+---+---+---+---+
|          1 | which of these are colors?                       | 1 | 0 | 0 | 1 |
|          2 | which of these are fruits?                       | 1 | 1 | 1 | 0 |
|          3 | which of these are programming language?         | 0 | 1 | 1 | 0 |
|          4 | What is IPv6?                                    | 0 | 0 | 0 | 1 |
+------------+--------------------------------------------------+---+---+---+---+

用户的答案表:(1表示用户选择该答案,但可能是未解决的)

+------------+--------+---+---+---+---+
| questionID | userID | a | b | c | d |
+------------+--------+---+---+---+---+
|          1 |      1 | 1 | 1 | 0 | 0 |
|          2 |      1 | 1 | 1 | 1 | 0 |
|          1 |      3 | 1 | 0 | 1 | 1 |
|          1 |      4 | 1 | 1 | 1 | 0 |
|          3 |      1 | 1 | 0 | 1 | 1 |
|          4 |      1 | 1 | 0 | 1 | 1 |
|          1 |      2 | 1 | 1 | 0 | 0 |
|          2 |      2 | 0 | 1 | 0 | 1 |
|          3 |      2 | 0 | 1 | 1 | 1 |
|          4 |      2 | 1 | 1 | 0 | 1 |
|          2 |      3 | 1 | 0 | 0 | 1 |
|          3 |      3 | 1 | 0 | 1 | 1 |
|          4 |      3 | 1 | 0 | 1 | 1 |
|          2 |      4 | 0 | 1 | 1 | 1 |
|          3 |      4 | 1 | 0 | 0 | 1 |
|          4 |      4 | 0 | 0 | 1 | 0 |
+------------+--------+---+---+---+---+

3 个答案:

答案 0 :(得分:1)

试试这个

webdriver.gecko.driver

答案 1 :(得分:0)

您加入问题和答案并添加正确的答案。每个答案的公式很简单:正确答案的数量除以5.您按照用户ID和SUM分组以获得每个用户的结果。

    // if you want want result from table for all condition true than write 
   //below query
    $sql= "SELECT id 
           FROM countries 
           WHERE country_code = LEFT($number,3)
           AND country_code = LEFT($number,2)
           AND country_code = LEFT($number,1)
           AND country_code = LEFT($number,0)" 

    // if you want result for OR condition

    $sql= "SELECT id 
           FROM countries 
           WHERE country_code = LEFT($number,3)
           OR country_code = LEFT($number,2)
           OR country_code = LEFT($number,1)
           OR country_code = LEFT($number,0)"   

在MySQL select u.userid, u.first_name, u.last_name, counted.points from ( select a.userid, sum(((a.a = q.a) + (a.b = q.b) + (a.c = q.c) + (a.d = q.d) + (a.e = q.e)) / 5) as points from question q join answer a on a.questionid = q.questionid group by a.userid ) counted join users u on u.userid = counted.userid; true = 1中,false = 0在正确时为1,在错误时为0。

答案 2 :(得分:0)

好问题...... !!!

我认为此查询将帮助您解决此方案中可能出现的所有可能情况。

SELECT 
u.userID, u.first_name, u.last_name, u.email, 
SUM(1 - ((!(answer.a = question.a)) + (!(answer.b = question.b)) + (!(answer.c = question.c)) + (!(answer.d = question.d))) * 0.25) AS Score 
FROM answer 
INNER JOIN 
user u ON answer.userID = u.userID 
INNER JOIN 
question ON answer.questionID = question.questionID 
GROUP BY answer.userID