Z toho, co mohu shromáždit, by mělo stačit následující, pokud jsou pole tím, co jste poskytli.
INSERT INTO Address (email)
SELECT User.email
FROM User JOIN person ON User.id_person = person.id
WHERE person.id_address IS NULL
;
UPRAVIT (pomocí kurzoru)
S kurzorem by to mělo být docela jednoduché, ale vřele vám doporučuji, abyste se s těmito a jejich důsledky seznámili.
DROP PROCEDURE IF EXISTS _tmp_update_address;
DELIMITER $$
CREATE PROCEDURE _tmp_update_address()
BEGIN
DECLARE cursor_List_isdone BOOLEAN DEFAULT FALSE;
DECLARE cur_userId, cur_personId INT;
DECLARE cur_email VARCHAR(250) DEFAULT '';
DECLARE cursor_List CURSOR FOR
SELECT User.id, person.id_address, User.email
FROM User JOIN person ON User.id_person = person.id
WHERE person.id_address IS NULL
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET cursor_List_isdone = TRUE;
OPEN cursor_List;
loop_List: LOOP
FETCH cursor_List INTO cur_userId, cur_personId, cur_email;
IF cursor_List_isdone THEN
LEAVE loop_List;
END IF;
INSERT INTO Address (email) VALUES (cur_email);
UPDATE person SET person.id_address = LAST_INSERT_ID()
WHERE person.id = cur_personId;
END LOOP loop_List;
CLOSE cursor_List;
END
$$
DELIMITER ;
CALL _tmp_update_address();