Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从一个表的内部联接中删除重复项_Sql - Fatal编程技术网

Sql 从一个表的内部联接中删除重复项

Sql 从一个表的内部联接中删除重复项,sql,Sql,我有下表 id | cId --------------- 1 | 1 2 | 1 3 | 2 4 | 2 我需要帮助查询,返回以下结果 id1 | id2 | cId ---------------------- 1 | 2 | 1 3 | 4 | 2 SELECT s1.id firstC, s2.id secondC FROM SingleTable s1 inner join SingleTable s2 on s1.cId = s2.cId 但我有重复的

我有下表

id | cId
---------------
1  | 1
2  | 1
3  | 2
4  | 2
我需要帮助查询,返回以下结果

id1 | id2 | cId
----------------------
1   | 2   | 1
3   | 4   | 2


SELECT s1.id firstC, s2.id secondC
FROM SingleTable s1 inner join SingleTable s2 on s1.cId = s2.cId
但我有重复的。我想要没有重复的gte结果

我的结果是:

id1 | id2 | cId
---------------
1   | 1   | 1
2   | 1   | 1
1   | 2   | 1
2   | 2   | 1
3   | 3   | 2
4   | 3   | 2
3   | 4   | 2
4   | 4   | 2

请提供帮助。

您只需按
cId
分组,然后为每个
cId
选择最高和最低的
id

SELECT cId, 
       min(id) as id1, 
       max(id) as id2
FROM SingleTable 
GROUP BY cId
使用附加条件(
s1.id
)删除重复项(
s1.id
):

选择s1.id firstC,s2.id secondC
从单表s1内部连接单表s2
打开(s1.cId=s2.cId和s1.id
Try
select distinct s1.id,…
@Andomar i Try distinct…您的目标是获取每个cId的“id”值列表还是其他?你应该做什么取决于你真正想要实现什么。获取预期输出的方法不止一种,但问题是该查询实际应该做什么?我的目标是为每个cId获取“id”列表,但在我的实际选项卡中,id和cId在我的实际表id中是guid,而cId是guid。如果在我的实际表id和cId中是guid,我需要在查询中添加什么条件?
SELECT s1.id firstC, s2.id secondC
FROM SingleTable s1 INNER JOIN SingleTable s2
                    ON (s1.cId = s2.cId AND s1.id < s2.id)