views:

33

answers:

2

Sorry for the worst question title ever but I find it hard to explain in a sentence what im having trouble with.

I am developing a user feedback system using ASP.NET and C#. I have multiple tables and am trying to populate dropdown lists so that the feedback can be filtered.

My tables:

CREATE TABLE tblModules
(
Module_ID nvarchar(10) PRIMARY KEY,
Module_Title nvarchar(MAX) NOT NULL
);

CREATE TABLE tblQuestions
(
Q_ID int PRIMARY KEY IDENTITY(1,1),
Question_Text varchar(1000) NOT NULL

);

CREATE TABLE tblFeedback
(
Submission_ID int PRIMARY KEY IDENTITY(1,1),
Username varchar(100) NOT NULL,
Domain varchar(50) NOT NULL,
DateTime_Submitted datetime NOT NULL
Module_ID nvarchar(10)
FOREIGN KEY (Module_ID) REFERENCES tblModules (Module_ID);
);

CREATE TABLE tblAnswers
(
Q_ID int NOT NULL,
Submission_ID int NOT NULL,
Answer_Text varchar(max),
FOREIGN KEY (Q_ID) REFERENCES tblQuestions(Q_ID),
FOREIGN KEY (Submission_ID) REFERENCES tblFeedback(Submission_ID)
);

I have 2 dropdown lists. 1st one is populated with all modules from a table. The 2nd needs to be populated with Questions from tblQuestions but only if any answers to it exist (therefore if the Question ID 'Q_ID' exists in tblAnswers).

I can get the selectedModuleID from the first dropdown list. I have a List of all Questions referenced by Q_ID in tblAnswers. How do I crossreference this list with the module ID?

Each feedback submission gets a Submission ID and Module ID.

I know its complicated. Any help would be awesome. Thanks

Frank

A: 

This should do the trick...

SELECT Q_ID, Question_Text tblQuestions a 
WHERE EXISTS (SELECT NULL FROM tblAnswers a WHERE a.Q_ID = q.Q_ID)
Yves M.
+1  A: 

You want:

  • questions that have answers
  • questions that have a module parent (via tblfeedback)

So, my guess at what you want:

SELECT
   *
FROM
   tblQuestions Q
WHERE
   EXISTS (SELECT *
      FROM
          tblAnswers A
          JOIN
          tblFeedback F ON A.Submission_ID = F.Submission_ID
      WHERE
          Q.Q_ID = A.Q_ID AND F.Module_ID = @moduleID)
gbn
I'm fairly inexperienced in SQL - this solution is great and works logically as I read thru it but I tried it in a separate sql file to see what it would return but I keep getting 'invalid column name' at the @moduleID, even when I hardcode moduleIDs I know I submitted feedback for. Any ideas?
Frank
Apologies!! I found the problem - me. I didn't put quotes around my moduleID. Thanks again. You are a wizard worthy of a fine beard
Frank