我需要一些帮助来从几个表中获取数据。
这是我的表格:
_______________ ______________ ___________ _______________ _____________
|_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;
这将返回如下行:
|什么是blablabla? |答案是...... | 2(已回答)| 10(总计 答案)|
遗憾的是,这只返回一个问题的所有行。有没有办法让所有行成为同一调查的一部分(基于surveyID)?
如果想要输出如下:
|什么是blablabla? |答案是...... | 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)。
答案 0 :(得分: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
这只是一堆左连接与内连接;并且我抽象出每个表的计数一次,所以它应该更快,而不是在每一行上做一个子选择。通过这种方式,它可以对所有行执行一次并完成。
答案 1 :(得分: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;