简体   繁体   English

MYSQL - 选择 MIN 和 MAX 年份并填补空白

[英]MYSQL - Select MIN and MAX year and fill the gaps

I want to use Highcharts (bar) with my database, but I am struggling how to bring the JSON using my select.我想在我的数据库中使用 Highcharts (bar),但我正在努力如何使用我的选择带来 JSON。

I have three levels: Gold, Silver, Platinum and I need to get the minimum year and maximum year and cover the gaps if that level is not present in some year我有三个级别:黄金、白银、白金,我需要获得最小年份和最大年份,如果某个年份没有该级别,我需要弥补差距

I have this select:我有这个选择:

select B.year, A.level, COUNT(B.id_level) as n_level from level AS A
inner join company_level AS B ON A.id_level = B.id_level 
GROUP BY B.id_level, B.year
ORDER BY B.year

My result:我的结果:

year    level       n_level
2010    Siver       1
2010    Platinum    2
2010    Gold        2
2011    Gold        1
2013    Gold        1
2014    Platinum    1
2015    Silver      1

In my result I don't have Silver and Platinum for 2011 and I would like to put the Silver and Platinum in the list but with number_level (n_level) with 0.在我的结果中,我没有 2011 年的 Silver 和 Platinum,我想将 Silver 和 Platinum 放在列表中,但 number_level (n_level) 为 0。

My PHP:我的PHP:

$rows = mysqli_query($this->conn, $query);
$bln = array();
$bln['name'] = 'Year';   
$row['name'] = 'Level'; 

$year =""; 
while ($r = mysqli_fetch_array($rows)) {
    if($year !== $r['year']){
        $bln['data'][] = $r['year']; 
    }
    $row['data'][] = $r['n_level']; 
    $year = $r['year']; 

}
$rslt = array();              

array_push($rslt, $bln);
array_push($rslt, $row);

print json_encode($rslt, JSON_NUMERIC_CHECK);

My PHP Result:我的 PHP 结果:

[{"name":"Year","data":[2010,2011,2013,2014,2015]},{"name":"Level","data":[2,2,1,1,1,1,1]}]

But my goal is:但我的目标是:

[{"name":"Year","data":[2010,2011,2013,2014,2015]},{"name":"Gold","data":[2,2,1,1,1]}, {"name":"Silver","data":[1,0,0,0,1]}, {"name":"Platinum","data":[2,2,1,1,1]}]

Thank you for any help.感谢您的任何帮助。

Result after @Tin Tran answer: @Tin Tran 回答后的结果:

$rows = mysqli_query($this->conn, $query);
                $bln = array();
                $bln['name'] = 'Year';   
                $row_gold['name'] = 'Gold'; 
                $row_platinum['name'] = 'Platinum';              
                $row_silver['name'] = 'Silver';                    

                while ($r = mysqli_fetch_array($rows)) {
                $bln['data'][] = $r['year'];
                $row_gold['data'][] = $r['gold'];
                $row_platinum['data'][] = $r['platinum'];              
                $row_silver['data'][] = $r['silver']; ''; 

                }
                $rslt = array();              

                array_push($rslt, $bln);
                array_push($rslt, $row_gold);
                array_push($rslt, $row_platinum);              
                array_push($rslt, $row_silver);             
                print json_encode($rslt, JSON_NUMERIC_CHECK);

This code will works really well with Highcharts.这段代码与 Highcharts 配合得非常好。 Thank you again @Tin Tran.再次感谢@Tin Tran。

I am not familiar php, but I think based on your goal of the JSON, this query might give you result you're after.我不熟悉 php,但我认为根据您对 JSON 的目标,此查询可能会给您带来您想要的结果。

SELECT B.year,
       SUM(A.level = 'Gold') as Gold,
       SUM(A.level = 'Silver') as Silver,
       SUM(A.level = 'Platinum') as Platinum
FROM level AS A
INNER JOIN company_level AS B ON A.id_level = B.id_level
GROUP BY B.year
ORDER BY B.year;

result:结果:

year    Gold    Silver  Platinum
2010    2       1       2
2011    1       0       0
2013    1       0       0
2014    0       0       1
2015    0       1       0

sqlfiddle -> http://sqlfiddle.com/#!9/59893/3 sqlfiddle -> http://sqlfiddle.com/#!9/59893/3

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

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