Skip to content

Instantly share code, notes, and snippets.

@nieldw
Created July 26, 2016 06:38
Show Gist options
  • Save nieldw/1d050091d3efcd6ccc1b2666fe0ce49a to your computer and use it in GitHub Desktop.
Save nieldw/1d050091d3efcd6ccc1b2666fe0ce49a to your computer and use it in GitHub Desktop.
A MySQL script to shuffle around the names in an employee table. Effectively it replaces the first and last names on each row with names randomly chosen from other rows.
SET SQL_SAFE_UPDATES = 0;
drop table if exists scramble;
create temporary table scramble (SELECT
employee_id.id,
first_name.first_name,
last_name.last_name
FROM
(
-- randomise employee id
SELECT eid.*,
@row_num1:=@row_num1 + 1 'row_num'
FROM
(SELECT
emp1.id
FROM
employee emp1
ORDER BY RAND()) eid, (SELECT @row_num1:=0) r) employee_id
JOIN
(
-- randomise first name
SELECT fn.*,
@row_num2:=@row_num2 + 1 'row_num'
FROM
(SELECT
emp1.first_name, emp1.employee_number
FROM
employee emp1
ORDER BY RAND()) fn, (SELECT @row_num2:=0) r) first_name ON employee_id.row_num = first_name.row_num
JOIN
(
-- randomise last name
SELECT ln.*,
@row_num3:=@row_num3 + 1 'row_num'
FROM
(SELECT
emp2.last_name, emp2.employee_number
FROM
employee emp2
ORDER BY RAND()) ln, (SELECT @row_num3:=0) r) last_name ON first_name.row_num = last_name.row_num);
UPDATE employee e
JOIN scramble scramble ON e.id = scramble.id
SET
e.first_name = scramble.first_name,
e.last_name = scramble.last_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment