Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: None  |  size: 1.00 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. storing multiple values in one field
  2. SELECT Students.ID,
  3.        Organizations.name
  4. FROM Students
  5. INNER JOIN StudentsMajors ON Students.ID = StudentsMajors.studentID
  6. INNER JOIN OrganizationsMajors ON StudentsMajors.majorID = OrganizationsMajors.majorID
  7. INNER JOIN Oranizations ON OrganizationsMajors.orgID = Organizations.ID
  8. WHERE Students.ID = 1
  9.        
  10. create table organization_majors (
  11.     organization_id int not null,
  12.     major_id        int not null,
  13.     primary key (organization_id, major_id)
  14. );
  15. create table student_majors (
  16.     student_id int not null,
  17.     major_id   int not null,
  18.     primary key (student_id, major_id)
  19. );
  20.        
  21. select s.id, s.name
  22. from students s join student_majors m on s.id = m.student_id
  23. where m.major_id = 1
  24.        
  25. select s.id, s.name
  26. from students s
  27. join student_majors sm on s.id = sm.student_id
  28. join organization_majors om on sm.major_id = om.major_id
  29. where om.organization_id = 11
  30. group by s.id, s.name
  31. having count(*) = (select count(*) from organization_majors where organization_id = 11)