2

我需要一些帮助来从几个表中获取数据。

这是我的表:

_______________   ______________   ___________  _______________  _____________
|_tblUsers____|   |_tblAnswers__|  |_tblAlt__|  |_tblQuestion_|  |_survey_____|
| userID      |   | answerAltID |  | altID   |  | questID     |  | surveyID   |
| username    |   | userID      |  | altText |  | questText   |  | surveyName |
|_____________|   |_____________|  |_questID_|  |_surveyID____|  |____________|

TblUsers 拥有系统中的用户列表,tblAnswers 拥有用户给出的所有答案,tlbAlt 拥有问题的备选方案,tblQuestion 拥有问题。还有一个名为 tblSurveys 的表,但这里不需要,因为 tblQuestion 中提到了 ID。

这是我到目前为止所拥有的:

SELECT
tblQuestion.questText,
tblAlt.altText,
Count(tblAnswers.answerID) as answers_count,
(SELECT COUNT(answerID) FROM tblAnswers, tblAlt 
WHERE tblAnswers.answerAltID = tblAlt.altID 
AND tblAlt.questID = " & CInt(questionID) & ") as total_count
FROM tblAlt, tblQuestion
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;

这将返回如下行:

| 什么是布拉布拉布拉?| 答案是... | 2 (已回答) | 10(总答案)|

不幸的是,这只返回一个问题的所有行。有没有办法获取属于同一调查的所有行(基于surveyID)?

如果希望输出是这样的:

| 什么是布拉布拉布拉?| 答案是... | 2 (已回答) | 10(总答案)| 调查名称 |

我想返回所有备选方案(有多少答案、总答案、相关问题和调查)。


更新:

这是我的输入:

SELECT tblalternativ.altTekst, tblalternativ.altID, Count(tblsvar.svarAltID) as antSvar, 
(SELECT COUNT(*) FROM tblsvar, tblalternativ 
WHERE tblsvar.svarAltID = tblalternativ.altID 
AND tblalternativ.altSpmID = " & CInt(lblQuestion.Tag) & ") as antTotal, 
(SELECT Count(*) FROM tblalternativ WHERE altSpmID = " & CInt(lblQuestion.Tag) & ") as spmTotal 
FROM(tblalternativ) LEFT JOIN tblsvar ON (tblalternativ.altId = tblsvar.svarAltID) 
WHERE(tblalternativ.altSpmID = " & CInt(lblQuestion.Tag) & ")
GROUP BY tblalternativ.altTekst ORDER BY tblalternativ.altID ASC

我的输出:

altTekst    altID   antSvar antTotal    spmTotal
Black         83         1      3              5
Green         84         1      3              5
Yellow        85         1      3              5
White         86         0      3              5
Pink          87         0      3              5

但这仅显示一个问题的统计数据。我想在一项调查中显示所有问题。因此,我需要获取altTekst该调查、问题名称和调查 ID 的所有信息。

我想:

spmTekst      altTekst  altID   antSvar antTotal    spmTotal   evalID
What is...    Black         83         1        3          5        1
What is...    Green         84         1        3          5        1
What is...    Yellow        85         1        3          5        1
What is...    White         86         0        3          5        1
What is...    Pink          87         0        3          5        1
Who is....    The king      88         2        3          3        1
Who is....    The pope      89         0        3          3        1
Who is....    The president 90         1        3          3        1
Which....     Shoe          91         2        3          2        1
Which....     Hat           92         1        3          2        1

换句话说,我想要来自同一调查中所有问题的统计数据(基于 evalID)。

4

2 回答 2

3

返回所有问题、答案文本、具有该答案的用户数以及提供的答案总数;每次调查。

Select TQ.QuestText, tAlt.altText, count(*) as Answers_count, suM(mTemp.Cnt) as total_count
FROM tblQuestion tq
LEFT JOIN tblAlt talt on Talt.QuestID = TQ.QuestID
LEFT JOIN tblAnswers ta on ta.AnswerAltID = talt.AltID
LEFT JOIN tblUsers tu ON Ta.UserID = TU.UserID
LEFT join tblAnswers ta2 on ta2.answeraltId = talt.altID
LEFT JOIN 
  (SELECT COUNT(*) cnt, questID 
   FROM tblAnswers 
   INNER JOIN tblAlt on AltID = AnswerAltID
   group by questID) mTemp 
  on mTemp.QuestID = talt.QuestID
WHERE tQ.SurveyID = 123 --Change this to your value
Group by TQ.QuestText, TAlt.AltText

这只是一堆左连接与内连接;我抽象出每个表的计数一次,所以它应该更快,而不是在每一行上进行子选择。这样,它对所有行执行一次并完成。

于 2012-05-07T22:14:28.540 回答
1

试试这个(根本没有优化,只是添加了调查部分):

SELECT    tblQuestion.questText, tblAlt.altText, 
          Count(tblAnswers.answerAltID) AS answers_count,
           (SELECT COUNT(answerAltID) FROM tblAnswers, tblAlt 
           WHERE  tblAnswers.answerAltID = tblAlt.altID AND 
                  tblAlt.questID = " & CInt(questionID) & ") as total_count,
          survey.surveyName
FROM      survey, tblQuestion, tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.answerAltID)
WHERE     tblAlt.questID = " & CInt(questionID) & " AND 
          tblQuestion.surveyID = survey.surveyID
GROUP BY  tblAlt.altText;

编辑:然后试试这个:

SELECT    tblQuestion.questText AS spmTekst, tblAlt.altText AS altTekst, 
          tblAlt.altID, 
          Count(tblAnswers.answerAltID) AS antSvar,
          COUNT(tblAlt.altID) AS antTotal,
          COUNT(tblQuestion.questID) AS spmTotal,
          survey.surveyID AS evalID
FROM      tblQuestion 
JOIN      survey ON (survey.surveyID = tblQuestion.surveyID)
JOIN      tblAlt ON (tblAlt.questID = tblQuestion.questID)
LEFT JOIN tblAnswers ON (tblAnswers.answerAltID = tblAlt.altID)
WHERE     tblAlt.questID = " & CInt(questionID) & " AND -- what really is this? review this
          survey.surveyID = '123' -- the value u want
GROUP BY  tblAlt.altText
ORDER BY  tblAnswers.answerAltID;
于 2012-05-08T18:40:37.300 回答