Finding combinations of specific values

。_饼干妹妹 提交于 2019-12-02 07:57:31

Try:

SELECT col1
FROM mytable
WHERE col2 IN (6, 7)
GROUP BY col1
HAVING COUNT(DISTINCT col2) = 2
Erwin Brandstetter

This is probably among the fastest solutions:

SELECT col1  -- already DISTINCT?
FROM   tbl t1
JOIN   tbl t2 USING (col1)
WHERE  t1.col2 = 6
AND    t2.col2 = 7;

Assuming a PRIMARY KEY or UNIQUE constraint on (col1, col2), like it's typically implemented. Else add DISTINCT.

There are many other ways to implement relational division. Here are some:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!