假设 id 正在增加并且没有名称的下一个条目属于同一个条目,那么相应地更新您的表将是最容易的。
就是这样:
Test data:
/*
drop table yourTable;
CREATE TABLE yourTable
(`id` int, `name` varchar(40), `tel` int, `email` varchar(20))
;
INSERT INTO yourTable
(`id`, `name`, `tel`, `email`)
VALUES
(1, 'john', 0241, 're@yah'),
(2, NULL, 0534, 're@rra'),
(3, NULL, 435, 'fd@rar'),
(4, 'geo', 43435, 're@eae'),
(5, NULL, 2347, 'ui@ear'),
(6, NULL, 678, 're@yaya'),
(7, 'anything', 789, 'whatever')
;
#*/
UPDATE yourTable yt1 INNER JOIN (
SELECT
yt.*,
COALESCE(name, @prev) AS newName,
CASE WHEN name IS NOT NULL THEN @prev:=name END
FROM
yourTable yt
, (SELECT @prev:=NULL) v
ORDER BY id
) yt2 ON yt1.id = yt2.id
SET yt1.name = yt2.newName;
SELECT * FROM yourTable;
结果:
id name tel email
1 john 241 re@yah
2 john 534 re@rra
3 john 435 fd@rar
4 geo 43435 re@eae
5 geo 2347 ui@ear
6 geo 678 re@yaya
7 anything 789 whatever
那么您的查询应该可以正常工作。