我有这样的数据记录,我想从以前的填充记录中更新空白记录
Id name page is
1. Snd 11
2. 11
3. Kiran. 11
4. Kiran. 11
5. 11
6. 11.
我要更新这样的数据
Id name page is
1. Snd 11
2. Snd 11
3. Kiran. 11
4. Kiran. 11
5. Kiran 11
6. Kiran 11.
答案 0 :(得分:2)
SELECT
t1.*,
(SELECT name FROM mytable WHERE name IS NOT NULL AND id <= t1.id ORDER BY id DESC LIMIT 1)
FROM mytable t1
SELECT
列表中的子查询为每个{ID1}值提供了所有非空的name
值,这些值在当前id
之前。之后,具有最高ID的记录将排在最前面。使用LIMIT 1
,这是唯一的结果。因此,这是最建议的非空值。
UPDATE
语句:
UPDATE mytable t
SET name = s.name
FROM (
SELECT
t1.id,
(SELECT name FROM mytable WHERE name IS NOT NULL AND id <= t1.id ORDER BY id DESC LIMIT 1)
FROM mytable t1
) s
WHERE t.id = s.id AND t.name IS NULL