我有三行数据:
temp_number tempdate
A12345 null
A12345001 '2018-01-01'
A12345002 '2018-01-02'
我想使用以下查询将A12345
上的tempdate设置为2018-01-02
:
update table_a1 set tempdate = (select max(tempdate) from table_a1 where
substr(temp_number,1,6) = 'A12345')
where temp_number = 'A12345'
上面的查询不起作用,我想使用max()
函数更新值,而不是给出任何实际值。
您的WHERE
子句错误,应该只更新NULL
记录:
UPDATE table_a1
SET tempdate = (SELECT MAX(tempdate)
FROM table_a1
WHERE temp_number LIKE 'A12345%')
WHERE temp_number = 'A1234' AND tempdate IS NULL;
您可以使用连接子查询来获取最大值
update table_a1 m
INNER JOIN (
select max(tempdate) max_date, substr(temp_number,1,6) temp1
from table_a1
where substr(temp_number,1,6) = 'A12345')
group by temp
) t on t.temp1 = m.tempdate
set tempdate = t.max_date