SQL: how to select common lines of one table on several column

六眼飞鱼酱① 提交于 2019-12-04 16:16:41

Just a bit of reversed thinking - group by page and count distinct pro values for each. Return rows that matches the total of distinct pro values

SELECT [page]
FROM a
GROUP BY [page]
HAVING COUNT(DISTINCT pro) = (SELECT COUNT(DISTINCT pro) FROM a)

SQLFiddle

EDIT: for the second problem, just replace = with '<' in the final line -> SQLFiddle

Fot the first part of the question, try this query:

SELECT DISTINCT t1.page FROM a t1
WHERE (SELECT COUNT(DISTINCT t2.pro) FROM a t2 WHERE
       t2.page = t1.page) = 
(SELECT COUNT(DISTINCT t3.pro) FROM a t3)

And the second query is the simple substraction from all page values:

SELECT DISTINCT t4.page FROM a t4
EXCEPT
SELECT DISTINCT t1.page FROM a t1
WHERE (SELECT COUNT(DISTINCT t2.pro) FROM a t2 WHERE
       t2.page = t1.page) = 
(SELECT COUNT(DISTINCT t3.pro) FROM a t3)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!