如何从表中删除重复的记录

时间:2015-06-15 07:51:42

标签: mysql sql oracle hive

我有一个包含以下列的表

NAME      FRIEND
----------------------
Apple     Flavour
Flavour   Apple
New       Banana
Banana    Flavour

我想删除具有相同组合的记录的列,例如 苹果,味道和味道,苹果是一样的。当我们重复这样的组合时,我只希望在两个记录中有一条记录。

3 个答案:

答案 0 :(得分:0)

您可以使用最低 GREATEST 功能来获取唯一组合。

例如,

SQL> WITH DATA AS(
  2  SELECT 'apple' a,   'flavour' b FROM dual UNION ALL
  3  SELECT 'flavour',   'apple' FROM dual UNION ALL
  4  SELECT 'new',   'flavour' FROM dual UNION ALL
  5  SELECT 'banana',   'fruit' FROM dual
  6  )
  7  SELECT least(a,b) a,
  8    greatest(a,b) b
  9  FROM data
 10  GROUP BY least(a,b),
 11    greatest(a,b)
 12  /

A       B
------- -------
banana  fruit
apple   flavour
flavour new

SQL>

答案 1 :(得分:0)

为避免重复,请先选择名称为<的所有行。朋友。否则,如果反向名称/朋友对不存在,也返回。

select *
from tablename t1
where t1.name < t1.friend
   or not exists (select 1 from tablename t2
                  where t1.name = t2.friend
                    and t1.friend = t2.name)

或者select distinct,使用greatestleast

select distinct least(name, friend), greatest(name, friend)
from tablename

答案 2 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE Test ( name, friend ) AS
          SELECT 'Apple', 'Flavour' FROM DUAL
UNION ALL SELECT 'Flavour', 'Apple' FROM DUAL
UNION ALL SELECT 'New', 'Banana' FROM DUAL
UNION ALL SELECT 'Banana', 'Flavour' FROM DUAL
/

DELETE FROM TEST
WHERE ROWID IN (
  SELECT MIN( ROWID )
  FROM   test
  GROUP BY
  least( name, friend ),
  greatest( name, friend )
  HAVING COUNT(1) > 1 )
/

查询1

SELECT * FROM TEST

<强> Results

|    NAME |  FRIEND |
|---------|---------|
| Flavour |   Apple |
|     New |  Banana |
|  Banana | Flavour |

使用MySQL效率不高:

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE Test (
  name VARCHAR(10),
  friend VARCHAR(10)
)
/

INSERT INTO Test
          SELECT 'Apple', 'Flavour' FROM DUAL
UNION ALL SELECT 'Flavour', 'Apple' FROM DUAL
UNION ALL SELECT 'New', 'Banana' FROM DUAL
UNION ALL SELECT 'Banana', 'Flavour' FROM DUAL
/

DELETE t1
FROM
  TEST t1
  INNER JOIN
  TEST t2
  ON ( t1.Name = t2.friend AND t1.friend = t2.name )
WHERE
  t1.Name > t1.friend
/

查询1

SELECT * FROM TEST

<强> Results

|   name |  friend |
|--------|---------|
|  Apple | Flavour |
|    New |  Banana |
| Banana | Flavour |