English 中文(简体)
查询结果不正确
原标题:Query result is not correct

我的查询结果有问题。 它没有在“ 质询” 字段中显示正确的行 。

例如,在数据库表中,如果它说明(不包括所有字段):

QuestionContent     Option Type    QuestionMarks     Answer   ....// other fields

What is 2+2         A-D            1                  D
What is 3+3         A-D            1                  B
What is 4+4         A-E            2                  C

那么为什么当我创建我的查询时,结果会显示这个:

QuestionContent     Option Type    QuestionMarks     Answer   ....// other fields

What is 2+2         A-D            1                  D
What is 3+3         A-D            1                  D
What is 4+4         A-D            1                  D

下面是我正在使用的查询, 如何固定此选项以显示正确结果?

SELECT q.QuestionContent, o.OptionType, q.NoofAnswers, a.Answer, r.ReplyType,
       q.QuestionMarks
FROM Answer a                
INNER JOIN Question q ON a.QuestionId = q.QuestionId
      JOIN Reply r ON q.ReplyId = r.ReplyId
      JOIN Question qu ON r.ReplyId = qu.ReplyId
      JOIN Option_Table o ON qu.OptionId = o.OptionId
GROUP BY q.QuestionContent

BELOW是四张表格的图案:

问题表:

SessionId (PK)  Varchar(3)
QuestionId(PK)  INT
QuestionContent Varchar(250)
NoofAnswers     INT
QuestionMarks   INT
ReplyId(FK)     Varchar(2)
OptionId(FK)    Varchar(2)

答复表格

  SessionId (PK)  Varchar(3)
    QuestionId(PK)  INT
    Answer          Varchar(10)

选项表(_表格)

  OptionId(PK)    Varchar(2)
  OptionType        Varchar(10)

答复表

  ReplyId(PK)    Varchar(2)
  ReplyType        Varchar(10)

在查询中,我想显示这些字段:

QuestionContent 
OptionType
NoofAnswers
Answer
ReplyType 
QuestionMarks 

希望这能提供足够的信息,

最佳回答

鉴于问题表的主要关键是会话ID + 问题ID,而答复表的主要关键也是会话ID + 问题ID,所以您必须在这两栏中指定这两个表格的组合:

FROM Answer   AS a
JOIN Question AS q ON q.SessionID = a.SessionID AND q.QuestionID = a.QuestionID

如果没有两者,当问题识别符出现在不止一次的会话中时,最终会产生笛卡尔产品效果。

此外,既然您没有选择会话代号,您将需要处理两场不同的会议都有相同问答信息的重复结果。 我认为SELECT DISTIST可能比按目的划分的组要好。 保留组按备案组分类,用于您拥有总和(如COUNT(*)或SUM(表达方式))时,而不用于一般的重复消除。


Original commentary

我认为,你的问题有两部分——其中一部分可能并不重要,另一部分可能至关重要。

您的查询或多或少是 :

SELECT q.QuestionContent, o.OptionType, q.NoofAnswers, a.Answer, r.ReplyType,
       q.QuestionMarks
FROM Answer a                
INNER JOIN Question q ON a.QuestionId = q.QuestionId
      JOIN Reply r ON q.ReplyId = r.ReplyId
      JOIN Question qu ON r.ReplyId = qu.ReplyId
      JOIN Option_Table o ON qu.OptionId = o.OptionId
GROUP BY q.QuestionContent

出于某种原因,你两次列出了问题,但是没有证据表明你需要第二个别名。仅在此基础上,你可以将查询简化为:

SELECT q.QuestionContent, o.OptionType, q.NoofAnswers, a.Answer, r.ReplyType,
       q.QuestionMarks
  FROM Answer       AS a                
  JOIN Question     AS q ON a.QuestionId = q.QuestionId
  JOIN Reply        AS r ON q.ReplyId = r.ReplyId
  JOIN Option_Table AS o ON q.OptionId = o.OptionId
 GROUP BY q.QuestionContent

这是可能不是关键的变化;您似乎有一个 1:1 的组合。 优化者也许能够忽略对问题表的多余的第二次引用,但没有提到未使用的内容更好。

很可能关键的问题是按条款分组。在SQL的大多数方言中,你需要按条款列出小组中所有非集合的选定值。在这个查询中,没有显示非集合值,这意味着列出所有6个结果列。如果你不这样做,MySQL会随意处理一些事情。我怀疑你需要披露整个查询中是否有任何汇总,并解释小组应该为你做什么。我不知道仅仅略去它是否会给你你需要的答案,或者你是否需要做其他事情。缺乏确定性的一个原因是问题缺乏计划;我们无法告诉所有关于表格的图案以及它们之间的主要关键、外在的关键关系。

因此,我建议使用(尝试):

SELECT q.QuestionContent, o.OptionType, q.NoofAnswers, a.Answer, r.ReplyType,
       q.QuestionMarks
  FROM Answer       AS a                
  JOIN Question     AS q ON a.QuestionId = q.QuestionId
  JOIN Reply        AS r ON q.ReplyId = r.ReplyId
  JOIN Option_Table AS o ON q.OptionId = o.OptionId

如果这给出了错误的行数, 那么您需要解释这些行的图案和关系 。

问题回答

问题:对于多种选择来说,答案总是1:1的比例。您没有理由不能将答案列放在问题表格中。您可以使用选项类型作为外国密钥,并将它与您各类可能的问题类型联系起来,正如您刚才提到的。

编辑:我为每个问题的多个答案重新设计。

所以设计像:

QuestionContent     QuestionID(PK)    QuestionMarks     AnswerID(PK)   ....// other fields

What is 2+2         1                    1                  1
What is 3+3         1                    1                  2
What is 4+4         2                    2                  3

和备选表格

QuestionID(FK)     Option Type

1         A-D           
2         A-E            
3         A-C    

现在的回答表格

AnswerID(Fk) Answer

1         A           
1         B
2         A            
3         C
3         A

现在留给您任意的答案类型, 允许每个问题多个答案 。

您正在从不同的表格中选择答案。 (a. amswers) 您应该做 q. Answer 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签