简体   繁体   English

MYSQL 不同后移动数据

[英]MYSQL moving data after distinct

I have data I am pulling from one table to another table but I have a Primary key on the ID column.我有数据要从一个表拉到另一个表,但我在 ID 列上有一个主键。 how do I pull the rest of the data after a Select distinct to make sure I don't have duplicates in the primary?如何在 Select 不同后提取数据的 rest 以确保我在主数据库中没有重复项?

INSERT INTO `goac`.`store`(`STORE_ID`)
select distinct STORE_ID
from ods_sale_large

例如我有这个

where I have gotten the distinct store_id from it and inserted into the table but I need the rest of the data to come with.我从中获得了不同的 store_id 并插入到表中,但我需要附带数据的 rest。

I guess it is in case of duplicate keys between existing records in the target table and the source query.我猜这是在目标表中的现有记录和源查询之间存在重复键的情况下。 You can use then INSERT.. ON DUPLICATE KEY UPDATE .然后您可以使用INSERT.. ON DUPLICATE KEY UPDATE As an example, If your table goac .例如,如果您的表goac store has a primary key on STORE_ID and a field DUPLICATE_STORE_ID , you can insert only the missing keys from ods_sale_large as new records and store the duplicate keys between ods_sale_large and goac . storeSTORE_ID上有一个主键和一个字段DUPLICATE_STORE_ID ,您可以只插入ods_sale_large中缺少的键作为新记录,并将重复的键存储在ods_sale_largegoac之间。 store in the field DUPLICATE_STORE_ID of the existing records: store在现有记录的DUPLICATE_STORE_ID字段中:

INSERT INTO `goac`.`store` (`STORE_ID`)
select distinct STORE_ID from ods_sale_large
ON DUPLICATE KEY UPDATE `DUPLICATE_STORE_ID` = VALUES(`STORE_ID`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM