比较一个查询中的两个表

时间:2012-11-21 17:06:12

标签: android sql

我正在构建一个小游戏,它可以获得最佳关卡和你完成的关卡数量,这比正确问题的一半还要多。我有一个查询检查这个,但问题我不知道如何整合问题表。如果用户没有尝试回答问题,则不会将任何行写入答案表。所以它实际上现在与每个级别的答案表中的行数进行比较。关于如何整合这个的任何想法? (count(*)/ 2)实际上

Cursor c = myDataBase
                .rawQuery(
                        "select level, (select count(*) from ( select level from answers group by level having sum (answercorrect) >= (count(*)/2) ) answers ) completedlevels, "
                                + "from answers "
                                + "group by level "
                                + "order by sum (score) desc ", null);

我尝试了这个,但它不起作用:

Cursor c = myDataBase
                .rawQuery(
                        "select level, (select count(*) from ( select level from questions group by level having sum (answercorrect) >= ((select count(*)/2 from questions group by level) ) answers ) completedlevels, "
                                + "from answers "
                                + "group by level "
                                + "order by sum (score) desc ", null);

修改 的 表结构是:

questions table
id    level   question
1      1      question1level1
2      1      question2level1
...  
30     1      question30level1
31     2      question1level2
32     2      question2level2
...  
70     2      question40level2
71
...

//注意:每个级别可以有不同的问题数量

答案表

id   question_id   player   answercorrect   score   attempts  
1     5             1        1              1000     1
2     10            1        1              900      1
3     7             2        0              700      3
4     10            2        0              500      3
5     13            2        1              100      1
6     8             1        1              800      2

...

2 个答案:

答案 0 :(得分:2)

SQL连接用于根据这些表中某些列之间的关系查询来自两个或多个表的数据。 您可以参考此网址,sql_join了解更多详情

例如

select level from question q
inner join answer a on (a.questionID = q.questionID)

它将问题表中的级别coloumn数据插入到具有相同questionID的答案表中。

你可以与JOIN关键字建立联系。

您也可以参考此a-visual-explanation-of-sql-joins

我认为你的表结构是错误的,它不是关系型的。我建议你使用以下结构。

enter image description here

通过以下查询,您可以找到有多少用户以核心方式回答问题,而不是您可以根据需要自定义查询。

SELECT     *
FROM       tbl_question 
INNER JOIN tbl_userAnswers ON tbl_question.id = tbl_userAnswers.userChoiceID
INNER JOIN tbl_correctAnswers ON tbl_question.id = tbl_correctAnswers.QuestionId and tbl_userAnswers.userChoiceID = tbl_correctAnswers.correctChoiceId

enter image description here

答案 1 :(得分:2)

您需要的是SQL Join。 可以找到简单的SQL连接示例here

假设您的表格中包含以下数据,

Employee Table:
LastName    DepartmentID
Rafferty      31
Jones         33
Steinberg     33
Robinson      34
Smith         34
John          NULL

Department table:
DepartmentID    DepartmentName
31              Sales
33              Engineering
34              Clerical
35              Marketing

如果您使用以下查询...

SELECT
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID;

WHERE子句中的语句称为连接条件,这是Equi-Join。 它会让你这样, 你明白了,

LastName   DepartmentID    DepartmentName
Rafferty      31           Sales
Jones         33           Engineering
Steinberg     33           Engineering
Robinson      34           Clerical
Smith         34           Clerical