[英]How to select unique records from mysql table
I have two tables 我有两张桌子
table1
id|col1|col2|col3
table2
id|colA|colB|colC
both table has is as primary key 两个表都有作为主键
col1 in table1 is unique and would not allow duplicate value. table1中的col1是唯一的,不允许重复值。 I am trying to migrate the data from table2 to table1.
我正在尝试将数据从table2迁移到table1。 The columns are mapped in the following manner
以下列方式映射列
table1.id => table2.id
table1.col1 => table2.colA
table1.col2 => table2.colB
table1.col3 => table2.colC
I Used the following query to migrate the data 我使用以下查询来迁移数据
inert into table1 select id,colA,colB,colC from table2
Mysql complain about colA having duplicate value. Mysql抱怨colA具有重复值。 Then I use
然后我用
insert into table1 select id,distinct(colA),colB,colC from table2
Mysql complain about bad syntax. Mysql抱怨语法错误。 Could you please help me writing the correct query to migrate data from table2 to table1.
您能否帮助我编写正确的查询以将数据从table2迁移到table1。
Thanks 谢谢
Actually, you can lose some data, while saving in this way. 实际上,以这种方式保存时,您可能会丢失一些数据。 Because If colB and colC are different for the same colA, only one of the rows will be saved.
因为如果colB和colC对于相同的colA不同,则将仅保存其中一行。
You can use this query: 您可以使用以下查询:
INSERT INTO table1
SELECT id,colA,colB,colC
FROM table2
GROUP BY colA;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.