This is one possible answer for first one.
UPDATE table t JOIN
( SELECT fname, mname, lname, count(*) as qty
FROM table
GROUP BY fname, lname
HAVING qty > 1
) sub
ON t.fname = sub.fname AND t.lname = sub.lname
SET t.mname = sub.mname
WHERE t.mname = '' and sub.qty = 2
;
UPDATE
Should not use CASE WHEN, Should use IF statements.
It handles RONALD VALE records.
UPDATE table t JOIN
( SELECT fname, mname, min(mname) minname, max(mname) mxname, lname, count(*) as qty
FROM table
GROUP BY fname, lname
HAVING qty > 1
) sub
ON t.fname = sub.fname AND t.lname = sub.lname
SET t.mname = IF(sub.qty = 2, sub.mname, IF(sub.qty > 2, sub.mxname, NULL))
WHERE t.mname is NULL OR LEFT(t.mname,1) = LEFT(sub.mxname, 1)
AND t.mname <> sub.mxname
;
UPDATE 2
# Update 1
UPDATE table t JOIN
( SELECT fname, mname, min(mname) minname, max(mname) mxname, lname, count(*) as qty
FROM table
GROUP BY fname, lname
HAVING qty > 1 ) sub ON t.fname = sub.fname AND t.lname = sub.lname SET t.mname = IF(sub.qty = 2, sub.mxname, IF(sub.qty > 2 AND minname = mxname, sub.mxname, NULL)) WHERE t.mname is NULL #OR LEFT(t.mname,1) = LEFT(sub.mxname, 1);
# Update 2
UPDATE table t JOIN
( SELECT fname, mname, min(mname) minname, max(mname) mxname, lname, count(*) as qty
FROM table
GROUP BY fname, lname
HAVING qty > 1 ) sub ON t.fname = sub.fname AND t.lname = sub.lname SET t.mname = IF(sub.qty = 2, sub.mxname, IF(sub.qty > 2, sub.mxname, NULL)) WHERE LEFT(t.mname,1) = LEFT(sub.mxname, 1) AND t.mname <> sub.mxname # reduce unnecessary tasks;
before
DANIEL J ABADI
DANIEL ABADI
DANIEL ABADI
DANIEL ABADI
ROBERT ABADI
ROBERT K ABADI
AMEY S BAILEY
AMEY SCHENCK BAILEY
KARL K KWON
KARL KWON
DINESH MAJETI
ADAM M SMITH
ADAM B SMITH
ADAM C SMITH
ADAM SMITH
ADAM SMITH
JACK A SMITH
JACK B SMITH
JACK SMITH
RONALD A VALE
RONALD D VALE
RONALD DAVID VALE
RONALD VALE
after
DANIEL J ABADI
DANIEL J ABADI
DANIEL J ABADI
DANIEL J ABADI
DANIEL J ABADI
ROBERT K ABADI
ROBERT K ABADI
AMEY SCHENCK BAILEY
AMEY SCHENCK BAILEY
KARL K KWON
KARL K KWON
DINESH MAJETI
ADAM M SMITH
ADAM B SMITH
ADAM C SMITH
ADAM SMITH
ADAM SMITH
JACK A SMITH
JACK B SMITH
JACK SMITH
RONALD A VALE
RONALD DAVID VALE
RONALD DAVID VALE
RONALD VALE