我有一个表T1,有两列C1和C2,在这些列中有数字,可以重复。 样品:
C1 | C2
-------
10 | 8
11 | 8
12 | 10
10 | 2
11 | 10
14 | 2
我需要在列C2中找到值为8和2的列C1的编号。在此示例中,查询应返回10。 我解释过了吗?有人可以帮助我吗?
答案 0 :(得分:3)
最简单的方法是
select
c1 from table_name
where c2 in (2,8)
group by c1
having count(*) = 2
答案 1 :(得分:0)
SELECT DISTINCT y.c1
FROM table_name x
JOIN table_name y
ON x.c1 = y.c1
WHERE (x.c2 = 8 AND y.c2 = 2) OR (x.c2 = 2 AND y.c2 = 8);