简体   繁体   English

MySQL COUNT,GROUP_CONCAT和GROUP BY一起

[英]MySQL COUNT,GROUP_CONCAT and GROUP BY together

I have a MySQL table like following. 我有一个如下的MySQL表。

IP Host Label BotName
1  A    good    aa
1  A    good    aa
2  C    good    bb
3  C    bad     cc
4  D    bad     dd
4  D    bad     ee
5  E    good    ff
5  E    good    gg

I want to get the output like following. 我想获得如下输出。

count Host BotName  Type
2     A    aa       good
1     C    bb       good
1     C    cc       bad
2     D    dd,ee    bad
2     E    ff,gg    good

Conditions are: 条件是:

  1. Count IPs groupby Host and Label 按主机和标签对IP组进行计数
  2. If the botnames are different concatenate them using commas. 如果僵尸名称不同,则使用逗号将它们连接起来。

Following is which I have tried, but couldn't get expected results. 以下是我尝试过的操作,但无法获得预期的结果。

$result2 = mysql_query("SELECT GROUP_CONCAT(BotName) name FROM (SELECT *, COUNT(IP) FROM mytable GROUP BY Host,Label)q");

Try this 尝试这个

SELECT COUNT(*) AS `count`,
       HOST,
       GROUP_CONCAT(distinct boatname) BotName,
       label as Type
FROM temp
GROUP BY host,label

SQL Fiddle SQL小提琴

Count and GROUP_CONCAT will get the values, but you need to group by the Host and label / type fields. Count和GROUP_CONCAT将获得值,但是您需要按主机和标签/类型字段分组。

SELECT  COUNT(*) AS `count`, Host, GROUP_CONCAT(DISTINCT Botname) AS Botname, label AS type
FROM name
GROUP BY Host, type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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