Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-08-2008, 08:19 AM Database problem
Junior Talker

Posts: 2
Name: John
Trades: 0
I need help with a routine to edit a student and course (class) record.
I have defined on my database 3 tables: student,course (class) and course_allocation.

student has the following fields:
studentNum
firstName
lastName

course has the following fields:
courseID
courseName
currentNumberofStudents

course_allocation has the following fields:
studentNum
courseID

When I edit a student record, as it is it edits the records on student and course_allocation tables.
Suppose if for some reason I need to change/move the student from class 101 (maths) to class 102 (physics).
I want to write a routine that updates the currentNumberofStudents field on the course table as well, besides
the student and course_allocation tables.
Example if class 101 has currently 5 students registered and class 102 has 2 students registered for it and
I update the records of one of the students and I move him to class 102, then the currentNumberofStudents field
on the table course should show 4 students for class 101 and 3 students for class 102.
But I am unable to do it, suggestions on how can I do it?
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
babai is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-08-2008, 08:39 AM Re: Database problem
Defies a Status

Posts: 1,606
Trades: 0
Am I doing your homework for you?



//With the move function update the record set currentNumberofStudents for the class being added to using a +1 factor
//mysql read field data
//field data +1
// write new value to field

// Write update currentNumberofStudents for the class losing the student using a factor of -1
//mysql read field data
//filed data -1
// write new value to field


This is just an outline. There are a few lines of code between the comments.

__________________
Colbyt

Please login or register to view this content. Registration is FREE

Last edited by colbyt; 12-08-2008 at 08:40 AM.. Reason: add smiley
colbyt is offline
Reply With Quote
View Public Profile
 
Old 12-10-2008, 04:21 AM Re: Database problem
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
omgz

First of all: you should not select, increment and write back to increase/decrease the value in a record field:
Code:
update tablename set columname = columname + 1 where id = N
Still this does not relate to your problem directly.

To solve your problem you should make your application to update only course_allocation table and let the database recalculate the number of students:
Code:
update course_allocation 
set courseID = $newCourseID 
where studentNum = $studentNum;

update course c 
join (
   select courseID, count(studentNum) numOfSt 
   from course_allocation
   group by courseID
) ca 
on c.courseID = ca.courseID 
set c.currentNumberofStudents = ca.numOfSt;
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Database problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.39411 seconds with 12 queries