INSERT INTO sys_role_menu (role_id, menu_id)
SELECT DISTINCT
  child_link.role_id,
  child.parent_id
FROM sys_role_menu AS child_link
JOIN sys_menu AS child
  ON child.id = child_link.menu_id
LEFT JOIN sys_role_menu AS parent_link
  ON parent_link.role_id = child_link.role_id
 AND parent_link.menu_id = child.parent_id
WHERE child.menu_type IN (1, 2)
  AND child.parent_id IS NOT NULL
  AND child.parent_id <> 0
  AND parent_link.menu_id IS NULL;
