我尝试从我的c#project中的2个数据库表中检索一些数据,即AnswersTable和QuestionsTable。AnswerTtable包含名为" QuestionID"和" UserId"我试图查询用户使用UserID和QuestionID尝试的每个问题。如果用户尝试了10个问题,并且每个问题都有唯一ID,我将如何使用QuestionID查询用户尝试的所有问题。 这就是我从AnswersTable
中检索questionId的方法String questionid;
Query = "SELECT * FROM AnswersTable WHERE UserId = '1'";
theReader = conn.ExecuteStatement(Query);
while (theReader.Read())
{
questionid = theReader["QuestionId"].ToString();
}
在检索或读取questionID后,我将其传递给下一个查询:
Query = "SELECT * FROM QuestionsTable WHERE Id = '" + questionid + "'";
theReader = conn.ExecuteStatement(Query);
while(theReader.Read())
{
// I know I meant to do something in here in order to retrieve all the 10 attempted
//questions using their Unique IDs, how do I retrieve each and every question using
// the questionIDs?
}
我希望这是有道理的,谢谢
答案 0 :(得分:0)
您可以通过加入两个表,使用以下查询获取特定用户(在本例中为用户ID = 1)的所有问题及其答案。这样您就不需要拥有两个数据读取器。希望这会有所帮助。
SELECT A.AnswerId
,A.UserID
,A.QuestionId
,Q.Question
,A.Answer
FROM AnswersTable A
INNER JOIN QuestionsTable Q ON Q.QuestionId = A.QuestionId
WHERE UserID = 1