gpt4 book ai didi

mysql - 从现有表创建新表,所有非重复且只有最后一个(最大主键)重复

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:20 24 4
gpt4 key购买 nike

我想从具有相同列的现有表创建一个新表。但问题是我现有的表有重复的行。我想将表中的所有数据插入到新表中,但重复行中只有一条记录。

例如:

id        name       other
1 Test1 User
2 Test1 User
3 Test2 User2

我想在新表中使用 id 2,3。希望我能让你明白。提前致谢。

最佳答案

我想你想要:

create table newtable as
select max(id) as id, name, other
from t
group by name, other;

或者,如果您有很多列:

create table newtable as
select t.*
from t
where t.id = (select max(t2.id) from t t2 where t2.name = t.name);

编辑:

根据评论,OP 的最终查询是:

create table new_tester
select t.*
from tester t
where t.id = (select max(t2.id)
from tester t2
where t2.name = t.name and t2.other = t.other
);

关于mysql - 从现有表创建新表,所有非重复且只有最后一个(最大主键)重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58574344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com