SQL Query来获取通用记录 [英] SQL Query to get common records
本文介绍了SQL Query来获取通用记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张如下表格
ID用户名组ID
1 venkat 2
2 venkat 3
3 ramu 1
4 ramu 2
使用sql语句我想检索所有在这两个groupids中可用的用户名2,3
在这种情况下,只有Venkat是可在groupid 2和3中使用的用户名
请帮助我
解决方案
试试这个:
SELECT userName
FROM tableA
WHERE groupId IN(2,3)
GROUP BY userName
HAVING COUNT (DISTINCT groupId)= 2;
OUTPUT | USERNAME |
| ---------- |
| venkat |
I have a table as below
ID Username GroupID
1 venkat 2
2 venkat 3
3 ramu 1
4 ramu 2
Using the sql statement I want to retrieve all username's that are available in both the groupids 2,3
In this case only Venkat is the username that's available in both groupid 2 and 3
Kindly help me
解决方案
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 |
这篇关于SQL Query来获取通用记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文