sql重复查询结果

时间:2013-03-15 11:18:23

标签: mysql sql

你好我有如下表:

Table: port 
tid    leftside    rightside 
1      2           3          
2      3           2
3      2           4
4      4           2
5      4           3
6      3           4
7      4           5
8      5           4
9      3           6
10     6           3

当我运行此查询时

SELECT * 
FROM port pt JOIN port p 
  ON (pt.leftside = p.rightside 
     and p.leftside <> 2) 
WHERE pt.rightside = 2

我得到了这个结果

tid    leftside    rightside 
6      3           4 
5      4           3
8      5           4
10     6           3

前两行代表相同的关系,尽管它们位于两侧。 如果存在重复记录,我希望我的查询只检索一行。 例如: 查询应返回到行

tid    leftside    rightside 
6      3           4

而不是

tid    leftside    rightside 
6      3           4 
5      4           3

这些行。

2 个答案:

答案 0 :(得分:0)

我在MS SQL中玩过它

with port as
(
select 1 as tid, 2 as leftside, 3 as rightside
UNION ALL
select 2, 3, 2
UNION ALL
select 3, 2, 4
UNION ALL
select 4, 4, 2
UNION ALL
select 5, 4, 3
UNION ALL
select 6, 3, 4
UNION ALL
select 7, 4, 5
UNION ALL
select 8, 5, 4
UNION ALL
select 9, 3, 6
UNION ALL
select 10, 6, 3
)
SELECT * 
FROM port pt JOIN port p 
  ON (pt.leftside = p.rightside 
     and p.leftside <> 2) 
WHERE pt.rightside = 2

并得到了这些结果 -

(pt.tid pt.leftside pt.rightside p.tid p.leftside p.rightside)
2   3   2   5   4   3
4   4   2   6   3   4
4   4   2   8   5   4
2   3   2   10  6   3

通过向WHERE子句添加in (subquery) ...

SELECT *
FROM port pt JOIN port p 
  ON (pt.leftside = p.rightside 
    and p.leftside <> 2) 
WHERE (pt.rightside = 2)
   AND p.tid in (SELECT max(chk.tid) FROM port chk
                 GROUP BY Case when leftside>rightside
                             then rightside else leftside end,
                          Case when leftside<rightside
                             then rightside else leftside end
                )

它摆脱了p.tid = 5的行,因为它不是匹配对的较高(Max)值。

说明:这列出了所有10行,左侧和右侧的位置越来越低。

SELECT tid,
  Case when leftside>rightside then rightside else leftside end as lower,
  Case when leftside<rightside then rightside else leftside end as higher
FROM port

因此,通过GROUPing on lower&amp; higher,并检索max(tid),我们得到更高的匹配对的tid。

答案 1 :(得分:0)

另一种方法是在leftside大于rightside时交换leftsiderightside。然后使用PARTITION BYRANK删除重复项:

SELECT tid,
       leftside,
       rightside
FROM
(
  SELECT p.tid,
         CASE WHEN p.leftside <= p.rightside
              THEN p.leftside
              ELSE p.rightside
         END AS leftside,
         CASE WHEN p.leftside > p.rightside
              THEN p.leftside
              ELSE p.rightside
         END AS rightside,
         RANK() OVER
         (PARTITION BY CASE WHEN p.leftside <= p.rightside
                            THEN p.leftside
                            ELSE p.rightside
                       END,
                       CASE WHEN p.leftside > p.rightside
                            THEN p.leftside
                            ELSE p.rightside
                       END
          ORDER BY p.tid ASC) AS rank
  FROM port pt JOIN port p 
    ON (pt.leftside = p.rightside
        AND p.leftside <> 2)
  WHERE pt.rightside = 2  
) subquery
WHERE rank = 1;

请参阅SQL Fiddle Demo