我有一张桌子:
create table a (page int, pro int)
go
insert into a select 1, 2
insert into a select 4, 2
insert into a select 5, 2
insert into a select 9, 2
insert into a select 1, 3
insert into a select 2, 3
insert into a select 3, 3
insert into a select 4, 3
insert into a select 9, 3
insert into a select 1, 4
insert into a select 9, 4
insert into a select 12, 4
insert into a select 1, 5
insert into a select 9, 5
insert into a select 12, 5
insert into a select 13, 5
insert into a select 14, 5
insert into a select 15, 5
go
(这里是我开始写的这个表和查询的SQLfiddle )
所有行页面的共同值
我正在寻找从此表中提取每列“pro”的公共列“page”。这是我们所期望的:
1 9
我尝试使用:
SELECT DISTINCT a.page FROM a WHERE a.page IN ( SELECT b.page FROM a as b WHERE b.pro <> a.pro )
但是这个查询返回每个“页面”,它们至少有一个公共值,这不是我们需要的。见下文 :
1 4 9 12
相反的查询又名不同的值至少一个但不是所有时间
我希望提取链接到一个或多个“pro”的“页面”,但对所有这些“页面”都不通用(这与上一个查询完全相反)
这是我们的期望:
2
3
4
5
12
13
14
15
我无法找到这两个查询的解决方案:'(有人可以帮我解决这些问题吗?
最好的祝福
编辑:SQLfiddle 网址