Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0A simple queston for a DB GuruA simple queston for a DB Guru
Previous
 
Next
New Post
4/13/2007 1:57 PM
 

Hi,

I'm working on a simple module and have hit a brick wall with what I presume is a simple db query. Ye I just can't figure it out.

Lets say you have a page that lists a dynamic checkbox list of languages and you want to filter a list of students by the checkboxes selected.

For example, I want to create an SQL query that retreives a list of the names of students who speak English AND French ONLY.   I dont want a query that returns English or French.

I would really appreciate any help with this. There has to be some folks out there that can see this straight way.

Cheers,

Colin

----------------------------------------------------------------------------

Here's a simple table structure example:

Table: Students - StudentId (PrimaryKey)

StudentId StudentName
1 John
2 Mary
3 Sean

Table: Languages - LanguageId(PrimayKey)

LanguageId LanguageName
1 English
2 French
3 German

 Table: StudentLanguage - StudentId, LangugeId (Composite PrimaryKey)

StudentId

LangugeId

1 1
1 2
2 3
3 1
3 2

 


Colin Mulcahy
E: colin_mulcahy@hotmail.com
 
New Post
4/13/2007 2:15 PM
 

SELECT studentName FROM Students WHERE StudentID IN (SELECT StudentID FROM StudentLanguage INNER JOIN Languages ON StudentLanguage.LanguageID = Languages.LanguageID WHERE LanguageName = 'English') AND StudentID IN (SELECT StudentID FROM StudentLanguage INNER JOIN Languages ON StudentLanguage.LanguageID = Languages.LanguageID WHERE LanguageName = French')

Not the fastest query but IMO the easiest to understand.


Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
New Post
4/13/2007 2:24 PM
 

SELECT Students.StudentName FROM Students
INNER JOIN StudentLanguage ON Students.StudentId = StudentLanguage.StudentId
INNER JOIN Languages ON StudentLanguage.LangugeId = Languages.LanguageId
where Languages.LanguageName = 'English' or Languages.LanguageName = 'French'
 

If you need to return result for more that one lanuguge add distinct after Select or it will return a record for each student

Example:

StudentName                                           LanguageName
-------------------------------------------------- --------------------------------------------------
John                                                            English
John                                                            French

 
New Post
4/13/2007 2:25 PM
 

Leupold,

Thanks for the reply.

If this was a dynamic query based on the lanuages selected and based on the sample you posted would you have to concatinate the following foreach checkbox selection ie, french, spanish, english etc ?

AND StudentID IN (SELECT StudentID FROM StudentLanguage INNER JOIN Languages ON StudentLanguage.LanguageID = Languages.LanguageID WHERE LanguageId = French')

Colin


Colin Mulcahy
E: colin_mulcahy@hotmail.com
 
New Post
4/14/2007 1:07 AM
 

You can do a pivot table of the studentlanguage table so that you have a column for each language, then select from the pivot table the combination you are looking for.

If you create this pivot table using a series of joins, I believe you might need to use LEFT JOIN instead of INNER JOIN, or else you risk not picking up all the possible records.

 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0A simple queston for a DB GuruA simple queston for a DB Guru


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out