
Untitled
By: a guest on
Jun 10th, 2012 | syntax:
None | size: 1.00 KB | hits: 20 | expires: Never
storing multiple values in one field
SELECT Students.ID,
Organizations.name
FROM Students
INNER JOIN StudentsMajors ON Students.ID = StudentsMajors.studentID
INNER JOIN OrganizationsMajors ON StudentsMajors.majorID = OrganizationsMajors.majorID
INNER JOIN Oranizations ON OrganizationsMajors.orgID = Organizations.ID
WHERE Students.ID = 1
create table organization_majors (
organization_id int not null,
major_id int not null,
primary key (organization_id, major_id)
);
create table student_majors (
student_id int not null,
major_id int not null,
primary key (student_id, major_id)
);
select s.id, s.name
from students s join student_majors m on s.id = m.student_id
where m.major_id = 1
select s.id, s.name
from students s
join student_majors sm on s.id = sm.student_id
join organization_majors om on sm.major_id = om.major_id
where om.organization_id = 11
group by s.id, s.name
having count(*) = (select count(*) from organization_majors where organization_id = 11)