Possibly a difficult problem to solve… Also I realize there may be more than one right answer here, but the main thing is really about how to automate the process.
I’ve got lots of pupils in a InnoDb table. In another table I’ve got their favourite choices for roommates.
- Most have chosen two or three favourite roommates.
- A few have chosen none.
- Some are chosen by up to four others.
- Some are chosen by none.
- Most pupils are chosen by one or two other pupil(s).
In the thirds table I have the rooms that are going to be filled. The rooms have from two to three beds. There aren’t any one-bed or four-bed rooms.
As far as constraints go, it all works. The problem is, how do I automate this process with MySQL (or other programming, preferably PHP) so that I make everybody happy?
I’ve made the assumption that the first roommate chosen is the one the pupil wants most, thus I’ve rated the choises from one to four based on the order of mention. Of course, sometimes this crash and two pupils have the same choice on number one. And then there is the problem of the one’s that aren’t chosen by anybody, but you can assume that they will be mature about it.
Well for starters, I would save a backup of the database, just in case your procedure making goes awry:
You’re going to need a join table for the rooms and pupils as well, if you don’t already have one. So something like:
Note that, while you did a good job describing your tables in your question, providing your exact DDL for your table definitions (the statements that replicate the table and relationships) helps people give answers to these types of questions. Just for future reference, nothing hurt here 🙂
From there, you can run the following type of script to create a stored procedure you could then call to arrive at a good solution
(Note: This is not actually going work for you! You will need to fix this up to make the matching better, and tailor it to your specific data types! Might even be a syntax error or two in this, I’m writing it a little blind. Learning to fish is better than getting a fish, though ^^)
That should get you on the right track at least! Let me know if anything needs more clarification, but like I said I’m not going to write whole algorithm exactly for you, use wikipedia and the mysql man pages to learn your way into Stored Procedure stardom! 😉
source of stored procedure skeleton: http://forums.mysql.com/read.php?98,358569,358569