题目是:有2个10G的数据库,存储了一些string. 2者之间有一些重复的数据。请把它们合并为一个数据库,并且去除重复。

限制:内存是4G

例如: DB1: cmu, ucb, stanford, nyu

DB2: ucsb, ucb, ucsd, cmu.

两者合并后,应该是: DB: cmu, ucb, stanford, nyu, ucsb, ucsd.

作法:把DB1分为5个小的数据库,分别是DB11, DB12, DB13, DB14, DB15

把DB2分为5个小的数据库,分别是DB22, DB22, DB23, DB24, DB25

把DB11 与 DB22, DB22, DB23, DB24, DB25 分别进行Union操作,生成DB11Merge.

把DB12 与 DB22, DB22, DB23, DB24, DB25 分别进行Union操作,生成DB12Merge.

......

最后再把DB11Merge, DB12Merge, DB13Merge, DB14Merge, DB15Merge 合并在一起即可

用以下语句即可:

mysql> insert into merge select * from persons2;

1. How do I merge two tables in Access while removing duplicates?

ref: http://stackoverflow.com/questions/7615587/how-do-i-merge-two-tables-in-access-while-removing-duplicates

以下是实验结果:

A UNION query returns only distinct rows. (There is also UNION ALL, but that would include duplicate rows, so you don't want it here.)

 mysql> select * from persons2;                                                  +-----------+

 | FirstName |

 +-----------+

 | zelin     |

 | qihao     |

 +-----------+

  rows in set (0.00 sec)

 mysql> select * from persons;

 +-----------+

 | FirstName |

 +-----------+

 | yu        |

 | zhixu     |

 | zelin     |

 +-----------+

  rows in set (0.00 sec)

 mysql> 

 mysql> select * from persons union select * from persons2;

 +-----------+

 | FirstName |

 +-----------+

 | yu        |

 | zhixu     |

 | zelin     |

 | qihao     |

 +-----------+

  rows in set (0.00 sec)

2. Join

顺便介绍几个DB常用的merge用的语句:

http://www.w3schools.com/sql/sql_join.asp

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.

The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met.

Let's look at a selection from the "Orders" table:

OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20

Then, have a look at a selection from the "Customers" table:

CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico
3 Antonio Moreno Taquería Antonio Moreno Mexico

Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, if we run the following SQL statement (that contains an INNER JOIN):

Example

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Try it yourself »

it will produce something like this:

OrderID CustomerName OrderDate
10308 Ana Trujillo Emparedados y helados 9/18/1996
10365 Antonio Moreno Taquería 11/27/1996
10383 Around the Horn 12/16/1996
10355 Around the Horn 11/15/1996
10278 Berglunds snabbköp 8/12/1996

Different SQL JOINs

Before we continue with examples, we will list the types the different SQL JOINs you can use:

    • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
    • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
    • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
    • FULL JOIN: Return all rows when there is a match in ONE of the tables

3. Full Join

在mysql中没有full join语句,我们需要用union:

mysql> SELECT * FROM persons LEFT JOIN persons2 ON persons.firstName=persons2.firstName UNION SELECT * FROM persons RIGHT JOIN persons2 ON persons.firstName=persons2.firstName;

+-----------+-----------+

| FirstName | FirstName |

+-----------+-----------+

| zelin     | zelin     |

| yu        | NULL      |

| zhixu     | NULL      |

| NULL      | qihao     |

+-----------+-----------+

4 rows in set (0.00 sec)

4.  REPLACE Syntax

使用replace语句也可以达到去重的效果。前提是,我们把想要去重的项目设置为primary key即可。

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...

Or:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...

Or:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for aPRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 13.2.5, “INSERT Syntax”.

【面经】Epic: 数据库去重的更多相关文章

  1. postgresql数据库去重方法

    数据库去重有很多方法,下面列出目前理解与使用的方法 第一种 通过group by分组,然后将分组后的数据写入临时表然后再写入另外的表,对于没有出现再group by后面的field可以用函数max,m ...

  2. MySQL数据库去重 SQL解决

    MySQL数据库去重的方法 ​ 数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下 看这条SQL DELETE consum_record FROM ...

  3. mysql数据库去重语句和不同表之间列的复制语句

    1.去重语句:DELETE FROM `v_klg_item` WHERE id NOT IN (SELECT * FROM (SELECT MAX(id) FROM `v_klg_item` GRO ...

  4. mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句

    http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases;           ...

  5. python scrapy爬虫数据库去重方法

    1. scrapy对request的URL去重 yield scrapy.Request(url, self.parse, dont_filter=False) 注意这个参数:dont_filter= ...

  6. GP开发示例:数据库去重

    这个例子专业讲解基于ArcEngine使用GP开发的过程及遇到的问题.更多GP使用方法:GP使用心得 功能需求:现在外业第一次数据(简称调绘.mdb)和第二次数据(简称检查.mdb)有重复.第二次是在 ...

  7. 面试题: mysql 数据库去重 已看1 不好使

    mysql去重面试总结 前言:题目大概是这样的. 建表: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 CREATE TABLE `test2` (   `id` ...

  8. MongoDB数据库去重

    查询: db.patents_texts.aggregate([ { $group:{_id:{Patent_num:'$Patent_num',Patent_name:'$Patent_name'} ...

  9. 数据库去重与join连表

    join连表删除的效率与检测存在之后删除的效率比,后者的效率低了很多

随机推荐

  1. C++混合编程之idlcpp教程(一)

    我是C++语言的忠实拥趸,由于在上学时经历了资源匮乏的DOS时代,对C/C++这种更加接近硬件的语言由衷的喜爱.一直以来也是已C++作为工作的语言,对别的语言那是不屑一顾.在java火爆流行的时候,没 ...

  2. 大熊君说说JS与设计模式之------单例模式Singleton()

    一,总体概要 1,笔者浅谈 顾名思义单例模式并不难理解,是产生一个类的唯一实例,在我们实际开发中也会使用到这种模式,它属于创建模式的一种,基于JS语言本身的语法特征, 对象直接量“{}”,也可以作为单 ...

  3. Eclipse配置详解(包括智能提示设置、智能提示插件修改,修改空格自动上屏、JDK配置、各种快捷键列表……)

    Eclipse编辑器基本设置 1.添加行号 在边缘处右键 2.改字体 字体的一般配置 3.去掉拼写错误检查 4.Java代码风格 代码格式化 Ctrl + Shift + F 之后点击右边的New按钮 ...

  4. C#异步将文本内容写入文件

    在C#/.NET中,将文本内容写入文件最简单的方法是调用 File.WriteAllText() 方法,但这个方法没有异步的实现,要想用异步,只能改用有些复杂的 FileStream.WriteAsy ...

  5. kali Linux系列教程之BeFF安装与集成Metasploit

    kali Linux系列教程之BeFF安装与集成Metasploit 文/玄魂 kali Linux系列教程之BeFF安装与集成Metasploit 1.1 apt-get安装方式 1.2 启动 1. ...

  6. Hexo搭建Github静态博客

    1. 环境环境 1.1 安装Git 请参考[1] 1.2 安装node.js 下载:http://nodejs.org/download/ 可以下载 node-v0.10.33-x64.msi 安装时 ...

  7. EF Code First Migration总结

    开启Migration 1. 通过 Tools->Nuget Package Manager->Package Manager Console 打开Package Manager Cons ...

  8. 自制一个能显示helloworld的最简单OS

    <自己动手写操作系统> org 07c00h mov ax,cs mov ds,ax mov es,ax call DispStr jmp $ DispStr: mov ax,BootMe ...

  9. 【原】关于使用jieba分词+PyInstaller进行打包时出现的一些问题的解决方法

    错误现象: 最近在做一个小项目,在Python中使用了jieba分词,感觉非常简洁方便.在Python端进行调试的时候没有任何问题,使用PyInstaller打包成exe文件后,就会报错: 错误原因分 ...

  10. 更新日志 - 关于 iOS9 设备的安装及其他优化

    新版 fir.im 上线整 3 周了,感谢你们对 fir.im 的关注和支持!无以言表,唯有做更好用的产品给大家.本周我们对新版做了以下的功能更新和 bug 修复: 功能更新 在使用过程中,请注意: ...