Doubt with mysql query bring results even if you have nothing

Asked

Viewed 64 times

0

I have the following query inside a javascript that generates a graphic:

<script>
  $(".sparkline_one").sparkline([<?php
    $sql="select 
    count(id) as total,
    DAY(dia_criacao) AS DAY,
    MONTH(dia_criacao) AS MONTH, 
    YEAR(dia_criacao) AS YEAR 
    from chamados where dia_criacao BETWEEN DATE_SUB(NOW(), INTERVAL 2 WEEK) AND NOW() AND id_empresa='".$_SESSION['clientelogado']['id_empresa']."' GROUP BY YEAR(dia_criacao), MONTH(dia_criacao), DAY(dia_criacao) order by dia_criacao asc";
    $query=mysqli_query($con,$sql);
    $n=0;
    while($result=mysqli_fetch_array($query,MYSQL_ASSOC)){
      $chamados[]=$result['total'];
      $dias[]="$n: '$result[DAY]/$result[MONTH]/$result[YEAR]'";
      $n++;
    }
    $chamados=implode(",", $chamados);
    echo $chamados;
    ?>], {
      type: 'bar',
      height: '125',
      barWidth: 13,
      tooltipFormat: 'Dia {{offset:offset}}<br>{{value}} chamado(s)',
      tooltipValueLookups: {
        'offset': {
          <?php
          $dias=implode(",", $dias);
          echo $dias;
          ?>
        }
      },

      colorMap: {
        '7': '#a1a1a1'
      },
      barSpacing: 2,
      barColor: '#26B99A'
    });
  </script>

It works well but has only one problem: If there is a day that there were no open calls it does not display the day because there are no results in my db. I wonder if it is possible for mysql to return the days when there are no calls in the query order and with value 0 ? or if there is no best way to do it ? ( I will have to make a loop to query each day separately ?)

  • Does the query return the days you have not called? If you return the date but the number of calls is NULL ou 0, you could be using the function in mysql ifnull. The following is an example link: http://www.mysqltutorial.org/mysql-ifnull/

  • no, only returns the days with call

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.