【发布时间】:2018-07-31 16:29:59
【问题描述】:
我有类似的表结构
用户表
编号 |名称
1 |约翰
2 |山姆
3 |约翰
4 |山姆
5 |约翰
现在我想在表的 name 列上添加唯一键约束,并通过附加时间戳来更新这些值,所以我的输出应该像
标识 |名称
1 |约翰
2 |山姆
3 |约翰1533051839
4 |山姆1533051840
5 |约翰1533051841
我正在尝试使用临时表来做到这一点?我能做到吗? 我正在尝试以下解决方案
drop table if exists temp_user;
create table temp_user(id int(20), name varchar(128));
insert into temp_user
select id, name
from user
group by user.name;
update user u
inner join temp_user temp_u ON temp_u.id = u.id
SET u.name = CONCAT(u.name, UNIX_TIMESTAMP());
drop table temp_user;
【问题讨论】:
-
什么数据库平台,你的解决方案有什么问题?你不说有没有错误或者别的什么。
-
我正在使用 MySQL,当我执行此操作时,我最终会用相同的字符串重命名所有名称。