开发者

How to get a field with a certain amount of sub-field in SQL?

I don't think i phrased the question correctly. I am trying to grab all the GroupID fields from a table that have more then 6 UserID fields:

Here's what my table "group_members" looks like

|  GroupID  |  UserID  |  role  |
_________________________________

|  22       |  02      |  role  |
|  22       |  03      |  rol开发者_运维百科e  |
|  25       |  01      |  role  |


select * from group_members
group by GroupID
having count(*) >6


Selecting all GroupID that have more than 6 UserID, or in other words GroupID that appear in 6 rows (and the UserIDs are not NULL):

SELECT GroupID
FROM group_members
GROUP BY GroupID
HAVING COUNT(UserID) > 6

Selecting all GroupID that have more than 6 DISTINCT UserIDs :

SELECT GroupID
FROM group_members
GROUP BY GroupID
HAVING COUNT(DISTINCT UserID) > 6


you need to use a GROUP BY and HAVING statement in your SQL. Also note, that the UserID column must be a number data-type (there are many variants, but in your case in your case probably an integer)

the SQL:

select GroupID 
FROM group_members
GROUP BY GroupID
HAVING count(*) >6

more info on group by statement: http://www.w3schools.com/sql/sql_groupby.asp more info on having statement: http://www.w3schools.com/sql/sql_having.asp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜