Mysql 按用户查找组的不同计数

Mysql 按用户查找组的不同计数,mysql,group-by,Mysql,Group By,我想在查询中解决这个问题@ 例如: id ip 1 1.2.3.3 2 1.2.3.3 3 1.23.42.2 4 4.4.2.1 我在找 ip count ids 1.2.3.3 2 1,2 1.23.42.2 1 3 4.4.2.1 1 4 我试过询问- select count(id) ,ip , id from table group by ip ; 它不起作用。如何解决此问题。您希望选

我想在查询中解决这个问题@

例如:

id  ip 
1   1.2.3.3
2   1.2.3.3
3   1.23.42.2
4   4.4.2.1 
我在找

ip        count    ids 
1.2.3.3   2       1,2 
1.23.42.2 1       3 
4.4.2.1   1       4 
我试过询问-

select count(id) ,ip , id from table group by ip ; 

它不起作用。如何解决此问题。

您希望选择组的ID作为文本列,该列将包含文本,例如“1,2” 如果是这样的话,您必须遍历

select ip, count(id) from table group by id
通过记住@ip和@count的游标,然后为每一行检索该组的所有ID

select id from table where ip = @ip
通过另一个游标遍历所有结果,并将结果存储在nvarchar@ids变量中,该变量看起来像“1,2”

然后在主游标循环中,您可以将每一行输出为
@ip、@count、@ids

您可以使用MySQL函数:

使用将完成以下操作:

SELECT ip, count(id) as count, group_concat(cast(id as char)) as ids
FROM table 
GROUP BY ip 

为什么要编写
cast(id为char)
而不是
id
?@truesoftwell在phpMyAdmin下我有一个用于id的blob字段,而不是char,所以casting会给出正确的结果
SELECT ip, count(id) as count, group_concat(cast(id as char)) as ids
FROM table 
GROUP BY ip