Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:28:20+00:00 2026-05-31T13:28:20+00:00

Possibly a difficult problem to solve… Also I realize there may be more than

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:28:21+00:00Added an answer on May 31, 2026 at 1:28 pm

    Well for starters, I would save a backup of the database, just in case your procedure making goes awry:

    mysql -h host -u username -p password database_name > database_name_backup.sql
    

    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:

    CREATE TABLE room_pupil (
      room_id int NOT NULL,
      pupil_id int NOT NULL
    );
    

    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 ^^)

    DELIMITER $$ 
    DROP PROCEDURE IF EXISTS RoommateMatching$$ 
    CREATE PROCEDURE RoommateMatching() 
    BEGIN 
      DECLARE no_more_pupils int DEFAULT 0;
      DECLARE current_pupil CURSOR FOR 
    SELECT pupil_id FROM pupils; 
      DECLARE CONTINUE HANDLER FOR NOT FOUND 
    SET no_more_pupils = 1; 
    
    /* create a temp table to hold the proposals */ 
    CREATE TABLE temp_matches ( 
      proposing_pupil_id int NOT NULL,
      proposed_pupil_id int NOT NULL 
    ); 
    OPEN current_pupil; 
    
    FETCH current_pupil INTO this_pupil; 
    
    REPEAT 
    
    /* Coalesce -1 in so its easy to tell whether they had choices picked or not */
    SELECT COALESCE(pupil_choice_id, -1) INTO next_favorite_roommate_id 
      FROM roommate_preference /* TODO Your second table, don't know its name */ 
      WHERE pupil_id = this_pupil; /* TODO since there may be more than one match here or none, might need another iterator */
    
    SELECT COALESCE(proposed_pupil_id, -1) INTO existing_proposal 
      FROM temp_matches
      WHERE proposed_pupil_id = next_favorite_roommate_id;
    
    IF existing_proposal < 0 THEN 
      INSERT INTO temp_matches(proposing_pupil_id, proposed_pupil_id) 
      VALUES (this_pupil, next_favorite_roommate_id); 
    END IF; 
    IF existing_proposal > 0 THEN
      /* TODO see if there's a better match for existing_proposal here, you write it! */
    END IF;
    FETCH current_pupil INTO this_pupil; 
    UNTIL no_more_pupils = 1 
    
    END REPEAT; 
    
    CLOSE current_pupil; 
    /* start an iterator on the matches and insert records into your room_pupil table! */
    /* TODO you write that code! */
    DROP TABLE temp_matches; 
    END$$ 
    DELIMITER; 
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: In trying to replicate this problem one more time to answer your questions
Possibly a silly question, but I'm having no luck finding an answer. Using Visual
a possibly simple problem, but weird why I have no idea how to do
My question is possibly a subtle one: Web services - are they extensions of
I'm possibly just blind, but is there a command line to specify conditional compilation
Since its possibly one of the most widely used methods of the Java language,
This is probably a very difficult problem to troubleshoot with the information I can
I've encountered with an difficult bug, that Im not quite sure if there is
Quite simple problem (but difficult solution): I got a string in PHP like as
This is a difficult problem to describe so please let me know if anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.