[英]How to update a column by running max() on the same column?
我有三行数据:
temp_number tempdate
A12345 null
A12345001 '2018-01-01'
A12345002 '2018-01-02'
我想在设置tempdate A12345
至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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.