raulgonzalez
02-25-2009, 01:59 PM
Hello,
at our community college we have courses that are identified by letters and numbers, example: introcuction to reading would be "READ0375M01" and that course is assigned to one instructor which we identified withe the unique instructor ID.
We have very few courses that require more than one instructor per course, meaning that in one classoom there could be 2 or 3 instructors at a time.
In that example, our database would ooks like this:
COURSE...........| INSTRUCTOR_ID
----------------------------------
READ0374M01 | Brad Pit
READ0374M01 | JFK
ENGL0375S02 | Drew Barrymore
FREN0374E02 | Drew Barrymore
SPAN0276N35 | Brad Pit
If I was to run use the "DISTINCT" statement I would get all records probably.
What I wanted to pull out is (all recoreds where the course has two intructors)
so my results would look like this:
COURSE...........| INSTRUCTOR_ID
----------------------------------
READ0374M01 | Brad Pit
READ0374M01 | JFK
can anyone point me to the right direction, I can't seem to think of anything right now.
Thanks in advanced
randombit
02-25-2009, 05:53 PM
You could try using the count() function. Like this:
SELECT count(`COURSE`) AS instructors, `COURSE`, `INSTRUCTOR_ID` from `courses_table_here` WHERE instructors =2
I've never really used it in a situation like this, but something along those lines should work
stdunbar
02-25-2009, 05:57 PM
This isn't the worlds best database design but you can do it with a query such as:
SELECT COURSE, INSTRUCTOR_ID
FROM the_table_name
where 1 < (select count(*) from
the_table_name a
where a.course_name = the_table_name.course_name)
group by COURSE, INSTRUCTOR_ID
You're basically doing a self join to get the count and then checking where that number is greater than 1.
This is not a free operation by any means. I'm not sure if this is course work or real work but the database design should really be rethought if this is not just a class. In that case, presumably there should be one to many foreign key relationship between a "courses" table and an "instructor" table.
Lastly, if you really have Drew Barrymore teaching a class, please PM me with the details as I'm signing up! :D
raulgonzalez
02-25-2009, 07:40 PM
Randombit and stdunbar,
I tried both of your examples and couldn't get it to work. stdunbar, you are right, the design is all wrong, but unfortunately I cannot do anything about it at this point.
I took a different approach and instead I retrieve all distinct courses,instructor_id ordered by course and then put the results in an array. Then I compared the previous results with the next result and did not print it unless the course appeard more than once in the array. I will re-try the scripts you provided me later as I think it is the better way, but for now below is the code I am using.
Also, Drew Barrymore is not really teaching a course here, but if she was I would be taking the French one, just for the French Kisses :)
<?php
session_start();
require_once('../include/connection.php');
?>
<?php
echo "<h3 face=verdana>Team Teaching:</h2>";
$query = "SELECT DISTINCT ClassSection_id, Instructor_id FROM students_fall2008 order by ClassSection_id ASC";
$result = mysql_query($query) or die(mysql_error());
// Print out result
$i=0;
while($row = mysql_fetch_array($result)){
$ClassSection_id[$i] = $row['ClassSection_id'];
$Instructor_id = $row['Instructor_id'];
$i++;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
$array_count = count($ClassSection_id);
$piggy = 0;
$i=0;
while($i < $array_count){
$the_course = $ClassSection_id[$i];
$first_the_course = $the_course;
if(($first_the_course != $first_the_course_old) || ($i == 0)) {
$piggy = 1;
}
else
{
$piggy++;
echo "Record " . $i . " -- " . $the_course . " is a team teached course by" . $piggy . " instructors<br>";
}
$first_the_course_old=$first_the_course;
$i++;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
<?php
mysql_close(); // CLOSE THE FILES.
?>
Snargleflap
02-26-2009, 12:59 PM
I took a different approach and instead I retrieve all distinct courses,instructor_id ordered by course and then put the results in an array. Then I compared the previous results with the next result and did not print it unless the course appeard more than once in the array.
Often times in the real world you have to go with what works, even though you'd like to do it better. Especially if you're under the gun to get a project done in a hurry.
When I have to do that, I'll leave comments in the code so that if I'm working on the application again in the future, if I have time I'll come back and address my work-around to see if I can do it better.
The holy grail in application development is to make it work well, and make it something that is fairly easy to work on for the next guy that comes along.
aradapilot
02-26-2009, 06:41 PM
the COUNT solution above works, but you would use something like this
SELECT COURSE,INSTRUCTOR
FROM TABLE
GROUP BY COURSE
HAVING COUNT(INSTRUCTOR)>1