简体   繁体   中英

How can I get total user in a group from database by PHP

I have 2 tables in database:

http://i.stack.imgur.com/nRJFf.png

How can I get total user for each group. ie: group 1: total are 2 users; group2: total are 2 users; group3: total is 1 user

You need normalization and never store comma-separated data. Consider the following

mysql> select * from user_table ;
+---------+---------------+
| user_id | user_group_id |
+---------+---------------+
|       1 | 1,2           |
|       2 | 2             |
|       3 | 1,3           |
+---------+---------------+
3 rows in set (0.00 sec)

mysql> select * from group_table ;
+----------+------------+
| group_id | group_name |
+----------+------------+
|        1 | a          |
|        2 | b          |
|        3 | c          |
+----------+------------+
3 rows in set (0.00 sec)

The above data is not normalized and to get the desired result out of these you need to use some in-efficient query as

select 
g.group_id,
count(*) as  total 
from group_table g 
left join user_table u on find_in_set(g.group_id,u.user_group_id) > 0 
group by g.group_id ;

+----------+-------+
| group_id | total |
+----------+-------+
|        1 |     2 |
|        2 |     2 |
|        3 |     1 |
+----------+-------+

Now lets do normalization and store user-group data in a different table as

mysql> select * from user_to_group ;
+---------+----------+
| user_id | group_id |
+---------+----------+
|       1 |        1 |
|       1 |        2 |
|       2 |        2 |
|       3 |        1 |
|       3 |        3 |
+---------+----------+

You can easily write different queries from these tables now and here are some examples

select group_id,count(*) as  tot from user_to_group group by group_id ;

+----------+-----+
| group_id | tot |
+----------+-----+
|        1 |   2 |
|        2 |   2 |
|        3 |   1 |
+----------+-----+

Joining the tables would even more easy

select 
g.group_id,
g.group_name,
count(*) as  tot 
from user_to_group ug 
join group_table g on g.group_id = ug.group_id 
join user_table u on u.user_id = ug.user_id 
group by g.group_id

+----------+------------+-----+
| group_id | group_name | tot |
+----------+------------+-----+
|        1 | a          |   2 |
|        2 | b          |   2 |
|        3 | c          |   1 |
+----------+------------+-----+
SELECT group_name, COUNT(*) FROM user_table u, group_table g WHERE u.user_group_id LIKE %g.group_id% GROUP BY g.group_name;

这应该可以工作,并为您提供所有组的列表以及其中有多少用户。

This is not an answer to your specific question but rather an alternative data structure proposal that might be better.

Introduce a new table members that looks like

# members
user_id  |  group_id
   1     |     1
   1     |     2
   2     |     2
   3     |     1
   3     |     3

Then you could SELECT group_id, count(*) FROM members GROUP BY group_id

+----------+----------+
| group_id | count(*) |
+----------+----------+
|        1 |        2 |
|        2 |        2 |
|        3 |        1 |
+----------+----------+

This structure might also make it easier for you to manage your memberships. user_id + group_id should be unique. And if supported let them be foreign keys.

I will recommend you to create a third table which holds the information about which users are in which groups.

CREATE TABLE users_in_groups 
(
   user_id INT
 , group_id INT
);

Then you can join like this:

SELECT 
   gt.group_id
 , count(ut.user_id)
FROM
   user_table AS ut
 , INNER JOIN users_in_groups AS uig ON uig.user_id = ut.user_id
 , INNER JOIN group_table AS gt ON gt.group_id = uig.group_id
GROUP BY
   gt.group_id
;

To use the table you have now will you have to do something like this (in mysql):

SELECT
       gt.group_id
     , count(ut.user_id)
FROM
   user_table AS ut
 , INNER JOIN group_table AS gt ON LOCATE(gt.group_id, ut.user_group_id) > 0
GROUP BY
   gt.group_id

Remember, when using group by, always locate what makes your group unique!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM