SQL Query to get common records

爷,独闯天下 提交于 2019-12-03 16:15:34

Try this:

SELECT userName
FROM tableA 
WHERE groupId IN (2, 3)
GROUP BY userName 
HAVING COUNT(DISTINCT groupId) = 2;

Check the SQL FIDDLE DEMO

OUTPUT

| USERNAME |
|----------|
|   venkat |

An alternate approach using a plain JOIN;

SELECT DISTINCT t1.username 
FROM MyTable t1 JOIN MyTable t2
  ON t1.username = t2.username AND t1.groupid=2 AND t2.groupid=3;

An SQLfiddle to test with.

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