AS之后在MySQL中使用IF条件

时间:2018-10-09 16:52:09

标签: php mysql

我想这样做;

$p1 = " [ ";    
foreach($db_found->query("SELECT DISTINCT netID, count(callsign) as callcount, year(logdate) as year,
                            month(logdate)-1 as month, day(logdate) as day
              FROM NetLog
             WHERE netcall = 'CARROLL'
               AND netID <> 0

             GROUP BY netID
             ORDER BY logdate ") as $stat){ 
                 if ($stat[year] == 2017 ){
                    $p2 = "{ x: new Date($stat[year],$stat[month],$stat[day]), y: $stat[callcount] },  ";
                 }else if ($stat[year] == 2018 ){
                    $p3 = "{ x: new Date($stat[year],$stat[month],$stat[day]), y: $stat[callcount] },  ";
                 }
             }

但是当我回显$ p3的输出时,我得到的只是;

[ { x: new Date(2018,9,8), y: 15 }, ]

这可以在MySQL中完成吗?如果没有,我将如何去做? 我在每个变量$ p2和$ p3中寻找的内容应该是这样;

{ x: new Date(2018,0,8), y: 8 }, { x: new Date(2018,0,15), y: 9 }, { x: new Date(2018,0,22), y: 11 }, { x: new Date(2018,0,29), y: 12 }, { x: new Date(2018,1,5), y: 11 }, { x: new Date(2018,1,12), y: 9 }, { x: new Date(2018,1,19), y: 14 }, { x: new Date(2018,1,26), y: 13 }, { x: new Date(2018,2,5), y: 11 }, { x: new Date(2018,2,12), y: 15 }, { x: new Date(2018,2,19), y: 23 }, { x: new Date(2018,2,26), y: 16 }, { x: new Date(2018,3,2), y: 17 }, { x: new Date(2018,3,9), y: 19 }, { x: new Date(2018,3,16), y: 22 }, { x: new Date(2018,3,23), y: 12 }, { x: new Date(2018,3,30), y: 20 }, { x: new Date(2018,4,7), y: 15 }, { x: new Date(2018,4,14), y: 18 }, { x: new Date(2018,4,21), y: 18 }, { x: new Date(2018,4,28), y: 22 }, { x: new Date(2018,5,4), y: 13 }, { x: new Date(2018,5,11), y: 12 }, { x: new Date(2018,5,18), y: 15 }, { x: new Date(2018,5,25), y: 19 }, { x: new Date(2018,6,2), y: 17 }, { x: new Date(2018,6,9), y: 15 }, { x: new Date(2018,6,16), y: 12 }, { x: new Date(2018,6,23), y: 13 }, { x: new Date(2018,6,30), y: 12 }, { x: new Date(2018,7,6), y: 12 }, { x: new Date(2018,7,13), y: 14 }, { x: new Date(2018,7,20), y: 16 }, { x: new Date(2018,7,27), y: 16 }, { x: new Date(2018,8,3), y: 15 }, { x: new Date(2018,8,10), y: 13 }, { x: new Date(2018,8,17), y: 19 }, { x: new Date(2018,8,24), y: 18 }, { x: new Date(2018,9,1), y: 14 }, { x: new Date(2018,9,8), y: 15 },

2 个答案:

答案 0 :(得分:2)

您可以在MySQL中使用CONCAT()

如果要将同一年份的所有结果连接到以逗号分隔的列表中,则可以使用GROUP_CONCAT()

但是,由于我们需要进行两个级别的汇总,一次要对NetID的所有呼号进行计数,然后再将一年的所有结果连接起来,因此我们需要使用嵌套查询。

foreach($db_found->query("
        SELECT year, GROUP_CONCAT(obj SEPARATOR ', ') AS obj
        FROM (
            SELECT year(logdate) as year,
                    CONCAT('{ x: new Date(', YEAR(logdate), ', ', MONTH(logdate)-1, ', ', DAY(logdate), '), y: ', COUNT(callsign), ' }') AS obj
              FROM NetLog
             WHERE netcall = 'CARROLL'
               AND netID <> 0
             GROUP BY netID
            ORDER BY logdate) AS x
        GROUP BY year ") as $stat){ 
    switch($stat['year']) {
    case 2017:
        $p2 = $stat['obj'];
        break;
    case 2018:
        $p3 = $stat['obj'];
        break;
    }
}

请注意,使用DISTINCT时不需要使用GROUP BY,因为分组可以确保永远不会重复。

答案 1 :(得分:1)

您可以重写代码以使其更简单:

$p1 = " [ ";
$p2 = []; //create empty array
$p3 = []; //create empty array
foreach($db_found->query("SELECT DISTINCT netID, count(callsign) as callcount, year(logdate) as year,
                    month(logdate)-1 as month, day(logdate) as day
      FROM NetLog
     WHERE netcall = 'CARROLL'
       AND netID <> 0

     GROUP BY netID
     ORDER BY logdate ") as $stat) { 

    $x = "new Date($stat[year],$stat[month],$stat[day])";
    $y = "$stat[callcount]";
    $json = "{ x: $x, y: $y },"
    if ($stat[year] == 2017 ){
        $p2[] = $json; //add to array
    } else if ($stat[year] == 2018 ){
        $p3[] = $json; //add to array
    }
}
$p2 = implode(',',$p2); //concat the array elements with ','
$p3 = implode(',',$p3);