|
-
Dec 10th, 2012, 03:05 PM
#1
Thread Starter
Frenzied Member
Question on sql
I have 2 tables one is called Question, and another one is called Section.
In Section, there's sectionID, sectionName
In Question, there's sectionID, questionID, and question
What I am trying to a ccomplish is to retrieve the sectionName then list the questions that are under that section out. I have tried inner join, left join, but I am getting the sectionName every single time I print each question out. I just want it to appear once. I was going to do group by, but it's saying my statement is not sum, max/min.
Section1
Question1
Question2
Section2
Question3
Question4
-
Dec 10th, 2012, 09:19 PM
#2
Re: Question on sql
I would suggest that what you're asking for should not be done at the database level. You should just perform a join in your SQL code and then the format you're asking for should be handled by application logic.
-
Dec 11th, 2012, 08:45 AM
#3
Re: Question on sql
I agree with jmcilhinney that this should be done in the application. Working off a suggestion from someone in another forum I put this together and I'm not pround of it. It seems to work though:
Code:
create table #Section(SectionID int, sectionname varchar(30))
create table #Question(sectionid int, QuestionID int, Question varchar(30))
insert into #section(sectionid, sectionname) values(1,'Math')
insert into #Question(sectionid,questionid,question) values(1,1,'what is 1 + 1')
insert into #Question(sectionid,questionid,question) values(1,2,'what is 2 + 2')
insert into #section(sectionid, sectionname) values(2,'History')
insert into #Question(sectionid,questionid,question) values(2,3,'Who is buried in Grant''s tomb')
insert into #Question(sectionid,questionid,question) values(2,4,'How long was the 100 days war')
create table #temp(sectionid int, questionid int, text varchar(100))
insert into #temp
select SectionID, 0 as QuestionID, sectionname 'text'
from #Section
union all
select SectionID, QuestionID, Question 'text'
from #Question
order by SectionID, QuestionID
select text from #temp
drop table #section
drop table #Question
drop table #temp
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|