If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > query on 3 tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-04, 16:06
PhilippeL PhilippeL is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
query on 3 tables?

Hi everyone,

I'm currently trying to develop a little ASP-VB/Access application for in-house surveys, and can't figure out the following. I have 3 tables:

tblSurveys:
-surveyId
-surveyName

tblQuestions:
-questionId
-questionName
-answerId1

tblSurveyQuestions:
-surveyId
-questionId

Now, let's say i need to find the list of questionName and answerId1 for surveyId = 1. I've tried the following query:

Code:
SQLsurveyQuestions="SELECT tblQuestions.questionName FROM tblSurveyQuestions WHERE surveyId = 1
INNER JOIN tblQuestions ON tblSurveyQuestions.questionId=tblQuestions.questionId"
But i get a syntax error message, most likely because of the "WHERE" clause. Would it be possible to first query the tblSurveyQuestions table to find the list of questionId, put that in a recordset, and then use that recordset to query the tblQuestions table? Could this work or is it impossible to use multiple lines from a recordset in a query?

I know that the normal way to do this would be to put a surveyId field in the tblQuestions table and forget about the tblSurveyQuestions table, but we'll be re-using the same questions over and over in different surveys, which is why there is a third table.

Anyone suggestions?

Thanks in advance,

Philippe
Reply With Quote
  #2 (permalink)  
Old 04-14-04, 11:40
PhilippeL PhilippeL is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
solution found, but new problem

Ok i found that the following query gives the right results:

Code:
SQLsurveyQuestions="SELECT tblQuestions.questionId, tblQuestions.questionName 
FROM (tblQuestions LEFT JOIN tblSurveyQuestions ON tblQuestions.questionID = tblSurveyQuestions.questionID)
LEFT JOIN tblSurveys ON tblSurveyQuestions.surveyID = tblSurveys.surveyID 
WHERE tblSurveys.surveyId=1"
Now my problem is that i need to get the answer names related to the anser Ids found with this query.

So i have the following tables:

tblSurveys:
-surveyId
-surveyName

tblQuestions:
-questionId
-questionName
-answerId1 (and 2, 3, 4, 5)

tblSurveyQuestions:
-surveyId
-questionId

tblAnswers:
-answerId
-answerName

I manage to get part of the answer with this query:
Code:
SQLsurveyQuestions="SELECT tblAnswers.answerId, tblAnswers.answerName 
FROM ((tblAnswers LEFT JOIN tblQuestions ON tblAnswers.answerId = tblQuestions.answerId1) 
LEFT JOIN tblSurveyQuestions ON tblQuestions.questionID = tblSurveyQuestions.questionID) 
LEFT JOIN tblSurveys ON tblSurveyQuestions.surveyID = tblSurveys.surveyID 
WHERE tblSurveys.surveyId=1"
But it can only give me the answer names for answerId1. How can I modify this to integrate answerId2, 3, etc? Should i use a loop on the query, or can i use the previously obtained recordset with a query only on the tblAnswers table?

Thanks for any help!

Philippe
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On