我想在查询@
中解决此问题代表:
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 ;
它不起作用。如何解决这个问题。
答案 0 :(得分:0)
您希望选择组的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
答案 1 :(得分:0)
您可以使用MySQL函数GROUP_CONCAT:
select ip, count(id) as count, GROUP_CONCAT(id) AS ids
from table
group by ip ;
答案 2 :(得分:0)
使用group_concat即可:
SELECT ip, count(id) as count, group_concat(cast(id as char)) as ids
FROM table
GROUP BY ip