将两列从一个表复制到另一个表,但只复制唯一值

时间:2012-10-09 17:42:31

标签: mysql sql

我有table1,其中有

MBID   |    Artist
__________________

123321   The Beatles
123214   Led Zeppelin
123321   The Beatles

如何将所有不同 MBID's及其对应的Artist名称复制到新表中,以便新表格只有{{1} }}的

MBID

我试过

 MBID   |    Artist
__________________

123321   The Beatles
123214   Led Zeppelin

但这给了我奇怪的组合,而不仅仅是不同的MBID

当我将 insert into table2 (MBID,artist) select distinct(table1.MBID),table1.artist FROM danktable 作为主索引时,我收到此查询的错误,因为我获得了非唯一MBID值。

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

您可以按照以下方式执行此操作:

 insert into table2 (MBID,artist) 
 select MBID,max(artist)
 from table1
 group by MBID
相关问题