Skip to content

Instantly share code, notes, and snippets.

@henderjon
Last active October 20, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henderjon/a4653d18f101fa9378ccf0d147c963fe to your computer and use it in GitHub Desktop.
Save henderjon/a4653d18f101fa9378ccf0d147c963fe to your computer and use it in GitHub Desktop.
DROP PROCEDURE IF EXISTS deleteBadProjectAssignments;
DELIMITER $$
CREATE PROCEDURE deleteBadProjectAssignments( )
BEGIN
DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS(
SELECT DISTINCT
utp.project_id
FROM
users_to_projects utp
INNER JOIN users u ON utp.user_id = u.user_id
INNER JOIN projects p ON utp.project_id = p.project_id
WHERE
utp.project_id NOT IN(
SELECT
utp2.project_id
FROM
users_to_projects utp2
INNER JOIN users u ON utp2.user_id = u.user_id
WHERE
u.user_role_id != 2
)
AND p.system != 1
GROUP BY
project_id ,
u.user_role_id
ORDER BY
utp.project_id
);
delete from users_to_projects where project_id in (
select project_id from table2
);
END $$
DELIMITER ;
CALL deleteBadProjectAssignments();
DROP PROCEDURE IF EXISTS deleteBadProjectAssignments;
@donatj
Copy link

donatj commented Oct 19, 2016

👍 but I'd test it on a test db first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment