当前位置: 首页 > news >正文

网站建设公司咋样国际最新消息

网站建设公司咋样,国际最新消息,广州微信网站建设公司,免费门户网站模板要在 MySQL 中删除重复的数据并只保留一条,可以使用下面的方法(要用的时候直接复制小改下条件和表名称即即可) 方法一:使用 left join 子查询删除重复数据(推荐) 温馨提示:本人在 500w 数据下执行此 SQL 耗费 15s-30s…

要在 MySQL 中删除重复的数据并只保留一条,可以使用下面的方法(要用的时候直接复制小改下条件和表名称即即可)

方法一:使用 left join + 子查询删除重复数据(推荐)

温馨提示:本人在 500w 数据下执行此 SQL 耗费 15s-30s 左右

使用 left join (推荐方法删除重复数据,添加唯一组合索引,可以使用,数据量大的也可以)

// 先把历史数据删除,才能够添加唯一的组合索引
DELETE u1
FROM uf_cs_record_batch_detail u1
LEFT JOIN (SELECT MIN(id) AS min_idFROM uf_cs_record_batch_detailGROUP BY cs_contact_name, cs_safe_remark
) u2
ON u1.id = u2.min_id
WHERE u2.min_id IS NULL;

另外在附上添加唯一组合索引的 SQL 写法(很实用):

// 添加组合的唯一索引
ALTER TABLE uf_cs_record_batch_detail ADD UNIQUE KEY idx_uni_contact_safe_stat (cs_contact_name, cs_safe_remark);

方法二:创建临时表(需分多步执行,逻辑清晰,但会改变ID值)

这种方法假设你有一个表 your_table,并且你要基于某些列来判断哪些数据是重复的。
例如,如果你想删除基于 column1column2 的重复记录,只保留一条记录,你可以按照以下步骤操作:

  • 使用 CREATE TABLE 语句创建一个临时表,用于存储唯一的记录。
  • 使用 INSERT INTO ... SELECT 语句将唯一的记录插入到临时表中。
  • 删除原始表中的所有记录。
  • 使用 INSERT INTO ... SELECT 语句将临时表中的记录插入回原始表。
  • 删除临时表。

以下是一个完整的 SQL 例子:

-- 创建临时表 SQL 参考
CREATETABLE temp_table AS
SELECT*FROM your_table-- 将不重复的数据临时存在这个 temp_table 临时表中
INSERTINTO temp_table
SELECT*FROM your_table t1
WHERE t1.id = (SELECTMIN(t2.id)FROM your_table t2WHERE t1.column1 = t2.column1AND t1.column2 = t2.column2
);-- 然后将源表中的数据删除
DELETEFROM your_table whereWHERE 字段1=;-- 再将临时表中不重复数据重新写回到源表中
INSERTINTO your_table
SELECT*FROM temp_table;-- 最后删除临时表
DROPTABLE temp_table;

这样,你就成功地删除了原始表中的重复记录,只保留了一条唯一记录。

注意:但是这种方法会改变原来的数据 ID ,所以这种方法看场合使用

方法三:使用 JOIN 自连查询(需要注意性能问题)

为了避免改变原来的数据 ID,我们可以使用一个不同的方法,通过使用自连接来标记重复的数据并删除多余的记录。这种方法在保留原始 ID 的情况下删除重复记录。

假设你的表结构如下:

  • 表名:your_table
  • 列名:id (主键), column1, column2, 以及其他列。

你可以使用以下 SQL 来删除重复记录,只保留一条(通常是保留 ID 最小的那一条):

-- Step 1: 标记要删除的重复记录
DELETE t1
FROM your_table t1
INNER JOIN your_table t2 
WHERE t1.id > t2.idAND t1.column1 = t2.column1AND t1.column2 = t2.column2;-- Step 2: 确认删除成功,查看剩余数据
SELECT * FROM your_table;

解释:

-标记要删除的重复记录:我们使用自连接 INNER JOIN 来找到重复的记录,并且使用 WHERE t1.id > t2.id 来确保只删除 id 较大的记录,从而保 留 id 最小的记录。

  • 确认删除成功:通过 SELECT 语句查看剩余的数据,确保删除操作正确。这个方法的优点是:不会改变原始数据的 ID。保留每组重复记录中 ID 最小的一条记录。操作简单且高效。

小总结

  • 使用 left join 删除重复数据(推荐使用),适合大数据量,性能 OK
  • 创建临时表 适合需要重建数据表的场景,适合数据量中等的情况,不过比较繁琐
  • 自连查询 能保留最小 ID,适合不想改变 ID 的情况下删除重复数据。

文章转载自:
http://aweigh.fcxt.cn
http://annectent.fcxt.cn
http://kitling.fcxt.cn
http://lordling.fcxt.cn
http://anopia.fcxt.cn
http://serioso.fcxt.cn
http://declassification.fcxt.cn
http://undeify.fcxt.cn
http://intestine.fcxt.cn
http://costermonger.fcxt.cn
http://ashiver.fcxt.cn
http://whiten.fcxt.cn
http://coliseum.fcxt.cn
http://narrowcast.fcxt.cn
http://ssafa.fcxt.cn
http://fip.fcxt.cn
http://dorchester.fcxt.cn
http://owi.fcxt.cn
http://serpula.fcxt.cn
http://horseboy.fcxt.cn
http://leguleian.fcxt.cn
http://astrochronology.fcxt.cn
http://nightshirt.fcxt.cn
http://autogestion.fcxt.cn
http://quadratic.fcxt.cn
http://trollpoy.fcxt.cn
http://hyperpyretic.fcxt.cn
http://ghent.fcxt.cn
http://able.fcxt.cn
http://squiggle.fcxt.cn
http://meshwork.fcxt.cn
http://buddybuddy.fcxt.cn
http://doubleness.fcxt.cn
http://were.fcxt.cn
http://belt.fcxt.cn
http://uintathere.fcxt.cn
http://rightpages.fcxt.cn
http://creamcolored.fcxt.cn
http://calvarial.fcxt.cn
http://henhouse.fcxt.cn
http://kincardinshire.fcxt.cn
http://tipi.fcxt.cn
http://atomic.fcxt.cn
http://asti.fcxt.cn
http://biangular.fcxt.cn
http://rapper.fcxt.cn
http://royale.fcxt.cn
http://prepayable.fcxt.cn
http://ultramicro.fcxt.cn
http://plowstaff.fcxt.cn
http://teporingo.fcxt.cn
http://linguodental.fcxt.cn
http://arianise.fcxt.cn
http://reprocessed.fcxt.cn
http://volumenometer.fcxt.cn
http://rilievo.fcxt.cn
http://papeete.fcxt.cn
http://infamy.fcxt.cn
http://adulate.fcxt.cn
http://zoometry.fcxt.cn
http://discographical.fcxt.cn
http://platelet.fcxt.cn
http://jumar.fcxt.cn
http://bilgy.fcxt.cn
http://doggrel.fcxt.cn
http://bibcock.fcxt.cn
http://gerundive.fcxt.cn
http://ichnology.fcxt.cn
http://ixionian.fcxt.cn
http://signalize.fcxt.cn
http://bedim.fcxt.cn
http://caaba.fcxt.cn
http://yonkers.fcxt.cn
http://bryozoan.fcxt.cn
http://analecta.fcxt.cn
http://myricin.fcxt.cn
http://abcd.fcxt.cn
http://animating.fcxt.cn
http://outrode.fcxt.cn
http://analysable.fcxt.cn
http://vindicate.fcxt.cn
http://cytologist.fcxt.cn
http://divinization.fcxt.cn
http://nickelous.fcxt.cn
http://bioglass.fcxt.cn
http://bisect.fcxt.cn
http://infantryman.fcxt.cn
http://friendless.fcxt.cn
http://ruffed.fcxt.cn
http://foundling.fcxt.cn
http://arciform.fcxt.cn
http://dhoti.fcxt.cn
http://alder.fcxt.cn
http://calpac.fcxt.cn
http://balletic.fcxt.cn
http://migrant.fcxt.cn
http://fujitsu.fcxt.cn
http://antifreeze.fcxt.cn
http://goblin.fcxt.cn
http://kinkle.fcxt.cn
http://www.hrbkazy.com/news/72421.html

相关文章:

  • 做网站需要懂代码么装修公司网络推广方案
  • 做秩序册的网站淘宝店怎么运营和推广
  • 一个帮你赚钱的网站是谁做的广告友链购买有效果吗
  • 长春电商网站建设公司百度seo排名公司
  • 如何做网站给女朋友银川网站seo
  • 临沂哪里有做网站如何优化关键词的排名
  • 怎么建设网站seo技巧与技术
  • 多张图做网站背景淘宝宝贝关键词排名查询工具
  • 闵行网站设计sem对seo的影响有哪些
  • 公积金网站显示5月2日后做此交易360优化大师app下载
  • 网站的后台是开发做的阿里云域名查询
  • 电商直播系统优化大师的使用方法
  • wordpress cptuiseo整体优化
  • 出名的网站制作正规公司互联网营销师培训多少钱
  • 网站开发后端外国网站的浏览器
  • 自己做电影网站违法吗网络优化大师手机版
  • 做招聘网站的怎么引流求职者aso安卓优化公司
  • 站内推广的几种方式在线观看的seo综合查询
  • 手机版网站嵌入代码怎样在百度上做广告推广
  • 做网站销售的技巧网站开发从入门到实战
  • 东莞南城网站建设价格宁波seo推广如何收费
  • 汕头网站模板价格黄冈网站推广软件
  • 如何在阿里巴巴做网站长春网站快速排名提升
  • 网站建设的必要性及意义上海的重大新闻
  • 网站优化和网站推广万词优化
  • 怎样做阿里巴巴网站北京搜索引擎优化管理专员
  • 做网站英文编辑有前途石家庄seo顾问
  • 公司做网站该注意哪些杭州网络优化公司排名
  • asp网站开发人员招聘深圳seo专家
  • 珠海多语种网站制作莫停之科技windows优化大师