选择count strings,然后将它们分组到饼图中

nwlls2ji  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(350)

我真的需要完成这项工作,我需要将它们按四个分组,我目前正在使用饼图为我的表显示每个项目的百分比,并使用此查询显示它们的计数:
这是我的完整代码:https://www.pastiebin.com/5be43c1f6ec73

$query = "SELECT DISTINCT trendx, COUNT(*) as counter FROM jeremy_table_trend GROUP BY trendx";

这就是我的查询结果

Escalate                        99
Not Supported                12809
PE.Win32.TRX.XXPE50FFF026       23
PE.Win32.TRX.XXPE50FFF027      102
PUA.Win32.TRX.XXPE50FFF026       1
PUA.Win32.TRX.XXPE50FFF027      16
PUA.Win32.TRX.XXPE50FFF027P0005  1
Ransom.Win32.TRX.XXPE50FFF026   96
Ransom.Win32.TRX.XXPE50FFF027  619
Rapid Proliferation            596
TROJ.Win32.TRX.XX.PE50FFF026     5
Undet                         1425
Undet - component               17
Undet - Feedback               712
Undet - FileSize                 9
Undet - M/P                     73
Undet - Maturity               146

这是我的piechart的输出,但我希望它们按4- Undetected, Supported,Escalate and Not Supported . 我该怎么做?请帮帮我

这是我的图表代码:

<!-- Chart code -->
<script>
var chart = AmCharts.makeChart("chartdiv", {
  "fontSize":15,
  "type": "pie",
  "startDuration": 0,
   "theme": "black",
  "addClassNames": true,
  "legend":{
    "fontSize":15,
    "position":"right",
    "marginRight":250,
    "autoMargins":false
  },
  "innerRadius": "0",
  "defs": {
    "filter": [{
      "id": "shadow",
      "width": "200%",
      "height": "200%",
      "feOffset": {
        "result": "offOut",
        "in": "SourceAlpha",
        "dx": 0,
        "dy": 0
      },
      "feGaussianBlur": {
        "result": "blurOut",
        "in": "offOut",
        "stdDeviation": 5
      },
      "feBlend": {
        "in": "SourceGraphic",
        "in2": "blurOut",
        "mode": "normal"
      }
    }]
  },
  "dataProvider": 
  [
  <?php while($row = mysqli_fetch_array($result)): ?>

    {
      "TRENDX": "<?php echo $row['trendx'] ?>",
      "Values": <?php echo $row['counter']; ?>
    },

  <?php endwhile; ?>
  ]
  ,
  "valueField": "Values",
  "titleField": "TRENDX", 
  "outlineAlpha": 0.4,
  "depth3D": 30,
  "balloonText": "[[title]]<br><span style='font-size:20px'><b>[[value]]</b> ([[percents]]%)</span>",
  "angle": 20,
  "export": {
    "enabled": true
  }
});

chart.addListener("init", handleInit);

chart.addListener("rollOverSlice", function(e) {
  handleRollOver(e);
});

function handleInit(){
  chart.legend.addListener("rollOverItem", handleRollOver);
}

function handleRollOver(e){
  var wedge = e.dataItem.wedge.node;
  wedge.parentNode.appendChild(wedge);
}
</script>

我在想这个主意,但我不知道如何把他们分成四组
对于undet:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Undet%';

对于升级:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Escalate%';

对于不支持的:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Not Supported%';

对于支持的:

select trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx NOT LIKE '%Not Supported%' OR '%Undet%' OR '%Escalate%';
8wigbo56

8wigbo561#

有几种方法可以解决这个问题,可以使用联合查询

select 'Undet' as trendx, COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Undet%'
union all
select 'Escalate', COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Escalate%'
union all
select 'Not Supported', COUNT(*) as counter from jeremy_table_trend WHERE trendx LIKE '%Not Supported%'
union all
select 'Supported', COUNT(*) as counter from jeremy_table_trend WHERE trendx NOT LIKE '%Not Supported%' OR '%Undet%' OR '%Escalate%';

或者可以使用case语句和子查询,比如

select status, count(*) from ( 
  select 
   case 
    when trendx like '%Undet%' then 'Undet'
    when trendx like '%Escalate%' then 'Escalate'
    when trendx LIKE '%Not Supported%' then 'Not Supported'
    else 'Supported'
   end as status
) x group by status

相关问题