查询从SQL中删除重复项

时间:2017-04-27 06:37:19

标签: mysql sql duplicates distinct

我在桌子上叫做距离。它有4列。 id,start_from,end_to 距离

我有一些重复的记录。从某种意义上说,重复记录,

start_from   |   end_to    | distance
Chennai        Bangalore     350
Bangalore      Chennai       350
Chennai        Hyderabad     500
Hyderabad      Chennai       510

在上表中, chennai到班加罗尔 班加罗尔到chennai 都有相同的距离< / strong>即可。所以我需要查询删除选择上的记录。

我想要一个像

一样的外出
start_from   |   end_to    | distance
Chennai        Bangalore     350
Chennai        Hyderabad     500
Hyderabad      Chennai       510

4 个答案:

答案 0 :(得分:2)

您可以使用以下查询来查找重复项:

SELECT LEAST(start_from, end_to) AS start_from, 
       GREATEST(start_from, end_to) AS end_to, 
       distance
FROM mytable 
GROUP BY LEAST(start_from, end_to), GREATEST(start_from, end_to), distance
HAVING COUNT(*) > 1

<强>输出:

start_from,   end_to,  distance
--------------------------------
Bangalore,    Chennai, 350

现在,您可以使用上述查询作为派生表来过滤掉重复项:

SELECT t1.*
FROM mytable AS t1
LEFT JOIN (
    SELECT LEAST(start_from, end_to) AS start_from, 
           GREATEST(start_from, end_to) AS end_to, 
           distance
    FROM mytable 
    GROUP BY LEAST(start_from, end_to), GREATEST(start_from, end_to), distance
    HAVING COUNT(*) > 1
) AS t2 ON t1.start_from = t2.start_from AND 
           t1.end_to = t2.end_to AND 
           t1.distance = t2.distance    
WHERE t2.start_from IS NULL

WHERE子句谓词t2.start_from IS NULL会过滤掉重复的记录。

<强>输出:

start_from  end_to     distance
--------------------------------
Chennai     Bangalore  350
Chennai     Hyderabad  500
Hyderabad   Chennai    510

答案 1 :(得分:2)

如果Chennai to BangaloreBangalore to Chennai之间没有差异,您可以试试这个:

select
    max(`start_from`) as `start_from`,
    min(`end_to`) as `end_to`,
    `distance`
from yourtable
group by
    case when `start_from` > `end_to` then `end_to` else `start_from` end,
    case when `start_from` > `end_to` then `start_from` else `end_to` end,
    `distance`

这是rextester中的demo

即使Chennai to Hyderabad为350也有效demo

如果您希望保留Bangalore to Chennai,则只需更改maxmin的位置:

select
    min(`start_from`) as `start_from`,
    max(`end_to`) as `end_to`,
    `distance`
from yourtable
group by
    case when `start_from` > `end_to` then `end_to` else `start_from` end,
    case when `start_from` > `end_to` then `start_from` else `end_to` end,
    `distance`

也是demo

case when将与大多数数据库兼容。

答案 2 :(得分:0)

在查询中设置字段顺序(使用值)有助于获得唯一的行:

select distinct
    case when start_from  > end_to then end_to     else  start_from end as _start,
    case when start_from  > end_to then start_from else  end_to     end as _end,
    distance
from distance;

经过测试后我得到了:

+-----------+-----------+----------+
| _start    | _end      | distance |
+-----------+-----------+----------+
| Bangalore | Chennai   |      350 |
| Chennai   | Hyderabad |      500 |
| Chennai   | Hyderabad |      510 |
+-----------+-----------+----------+

答案 3 :(得分:0)

假设你的表喜欢

Select 
    O.start_from,
    O.end_to,
    O.distance 
From 
    distance O
Left Join
    distance P
On 
    1 = 1
    and O.start_from = P.end_to 
    and O.end_to = P.start_from
Where 
    1 = 1
    and O.distance <> P.distance 
    or(O.distance = P.distance and O.id < P.id)

然后你可以使用查询比较id。

{ path: 'jobs', loadChildren: () => System.import('./jobs/jobs.module') }