将两列从一个表复制到另一个表,但只能复制唯一值 [英] Copy two columns from one table to another, but only unique values

查看:96
本文介绍了将两列从一个表复制到另一个表,但只能复制唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有table1,其中有

MBID   |    Artist
__________________

123321   The Beatles
123214   Led Zeppelin
123321   The Beatles

如何将所有与众不同的 MBID's及其相应的Artist名称复制到新表中,以便新表仅具有不同的MBID

How can I copy all of the distinct MBID's along with their corresponding Artist name, into a new table, so that the new table only has distinct MBID's

 MBID   |    Artist
__________________

123321   The Beatles
123214   Led Zeppelin

我尝试过

 insert into table2 (MBID,artist) 
 select distinct(table1.MBID),table1.artist 
 FROM danktable

但这给了我奇怪的组合,而不仅仅是独特的MBID's

But this gives me weird combinations and not only distinct MBID's

当我将MBID用作主索引时,此查询出现错误,因为我得到的是不唯一的MBID值.

When I make the MBID a primary index, I get an error with this query because i'm getting non-unique MBID values.

有人可以帮我吗?

谢谢!

推荐答案

您可以按照以下步骤进行操作:

You can do it as follows :

 insert into table2 (MBID,artist) 
 select MBID,max(artist)
 from table1
 group by MBID

这篇关于将两列从一个表复制到另一个表,但只能复制唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆