3
I’m making a chart that lists a certain amount per month, but can have mès that has nothing, with this returns me no value and does not list in the chart.
I don’t know if I should make the change in the query or programming, can anyone help me?
My query looks like this:
SELECT DATE_FORMAT(dt_validade, '%y-%m') as anoMes, count(*) as qtde
FROM tabela
WHERE dt_validade > '{$today}'
GROUP BY anoMes
ORDER BY anoMes
her return is more or less that
anoMes | qtde 14-08 | 2 14-09 | 5 14-12 | 10 14-12 | 10 14-12 | 10 15-01 | 1 15-03 | 1
and with that, in php I run a loop in the result, I play the dice for an array that sends a json_encode for the data to the graph, but with this will only show me in the graph data of the months 08, 09 and 12, but I need to show also of the months 10 and 11 with zero values.
And in short, what I did in programming was this:
$cols = array(
array(
'Período',
'Quantidade'
)
);
$rows = array();
$query = "A query passada";
$result = $query->result();
foreach ($result as $r) {
array_push($rows, array(
$r->mesAno,
(int) $r->qtde
));
}
$dados = array_merge($cols, $rows);
return json_encode($dados);
And as a suggestion given to me, the expected result is this:
anoMes | qtde
14-08 | 2
14-09 | 5
14-10 | 0
14-11 | 0
14-12 | 10
15-01 | 1
15-03 | 1
Follow link with build on SQLFIDDLE, so I guess I can get a little clearer
I really can’t understand your question. How else could you send a query to BD but through SQL? If the question is something like CURRENT_DATE vs date("2014-01-01"), it matters little! Your query will be sent only once.
– felipsmartins
I also see no other way to send my query to BD if not with sql, so much so that’s what I’m doing and I intend to do. But the point is that I need the result with every month even with the amount reset and I don’t know how to do it and I don’t even know if I do it in my query or in the programming.
– Marcelo Diniz
In this case, specifically, no matter the method. Choose the most practical.
– felipsmartins
And you can help me in what is the most practical, and how to do, being that I am thinking and trying and not getting a solution!? Thank you
– Marcelo Diniz
@Marcelodiniz - Since our colleagues had some doubts in understanding your problem, my suggestion is that you edit the post and add a 'expected result'.. example: 08-14 |2 ... 09-14 | 5 ... 10-14 | 0 ... 11-14| 0.. 12-14 | 10 I think it would be clearer.
– emanuelsn