我有一张桌子:
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和我开始编写的查询)
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
反向查询也称为不同值,至少一次但并非始终如此
这是我们期望的:
2 3 4 5 12 13 14 15
我无法找到这两个查询的解决方案:’(有人可以帮我解决那些问题吗?
最好的祝福
编辑:SQLfiddle URL
只是一些相反的想法-分组page并pro为每个计数不同的值。返回与不同pro值总和匹配的行
page
pro
SELECT [page] FROM a GROUP BY [page] HAVING COUNT(DISTINCT pro) = (SELECT COUNT(DISTINCT pro) FROM a)
SQLFiddle
编辑:对于第二个问题,只需=在最后一行-> SQLFiddle中 替换为’<’
=