So I'm needing to pull 2 id fields from 2 different tables and insert them into a third table. This is for a permissions thing, so basically I need to get the admin table's id and loop that until all of the building table's ids are populated into the third table called building_user. This is to "start" the permissions table so I need every building ID to be matched up with every admin ID.
Here is what I'm trying and I can't quite figure it out, but I know I'm missing something.
CREATE PROCEDURE buildingUserAssignment()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE done2 INT DEFAULT 0;
DECLARE a, b INT;
DECLARE admin CURSOR
FOR
SELECT id FROM admin;
DECLARE building CURSOR
FOR
SELECT id FROM building;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN admin;
OPEN building;
REPEAT
FETCH admin into a;
REPEAT
FETCH building into b;
INSERT INTO building_user VALUES (a,b);
UNTIL done END REPEAT;
UNTIL done END REPEAT;
CLOSE admin;
CLOSE building;
END;
I end up only getting one admin ID field (ID #2, even though there is a ID 1). Here is the sample databases:
admin:
SELECT id FROM admin;
1
2
3
5
6
7
9
11
building:
SELECT id FROM building;
0
1
3
6
7
8
10
Current End Result of invoking buildingUserAssignment:
SELECT * FROM building_user:
user_id building_id
2 0
2 1
2 2
2 3
2 4
2 4
3 4
5 4
6 4
7 4
9 4
11 4
So the building_user table should be something like this in the end, repeated of course for each admin id:
1 0
1 1
1 3
1 6
1 7
1 8
1 10
I'm sure its something stupid, but I think I've looked at it too long and now I'm stuck. I may not even be doing this the "best" way either, so I welcome any suggestions.
select into building_user user_id, building_id from building cross useror something similar.