title: Split Records Equally Into Groups
body: <p>I'm wanting to build a schedule program(I want to write the sql to do it then I will build  a php site to execute it). For starters I would like to split up the students I have at my school between the teachers. I need them to be gender balanced, meaning equal or within say no more than 30% of one gender over the other in each class.</p>
  
  <p>So I have a students Table with all grade levels 1-5 and a teachers table with a field that designates what grade they teach. I then have a courses table that has the course that each teacher teaches, it contains a field that has the class size limit (only 18 students allowed etc) as well as the teacher ID who is teaching the course.</p>
  
  <pre><code>Student Table Example:
  
  Student_ID  Grade_Level
  253486           1
  365896           2
  485785           3
  968654           4
  154278           5
  
  
  
  Teacher Table Example:
  
  Teacher_ID  Grade Taught
  253              1
  584              2
  985              3
  647              4
  254              5
  
  Course Table Example:
  
  Course_ID   Class_Limit Teacher_ID
  5007010      15            253
  5007011      15            584
  5007012      18            985
  5007013      18            647
  5007014      10            254
  </code></pre>
  
  <p><strong>What I would like to be the end result is have the records dump into a table that has three fields, The teacher id, the course id and the student id.</strong></p>
  
  <p>Once I get this figured out, I would like to eventually build on it and add more ways to divide them up.</p>
  
  <p>Incase you're wondering why I want to do this is because our current scheduler dosen't support this kind of scheduling, and I'm not sure when, if ever they will add it. But even just this simple step would save tons of time currently.</p>
  
  <p>I appreciate any input or help.</p>