我有一个包含以下列的表
NAME FRIEND
----------------------
Apple Flavour
Flavour Apple
New Banana
Banana Flavour
我想删除具有相同组合的记录的列,例如 苹果,味道和味道,苹果是一样的。当我们重复这样的组合时,我只希望在两个记录中有一条记录。
答案 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
,使用greatest
和least
:
select distinct least(name, friend), greatest(name, friend)
from tablename
答案 2 :(得分:0)
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效率不高:
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 |