I'm trying to display all the month names with the value of zero if the data doesn't exist in the MySQL database.
For example
Table: invoice_order
order_id | user_id | order_date | order_total_amount
1 | 1 | 01-01-2021 | 10000
2 | 1 | 02-02-2021 | 20000
MySQL Query
$query = "
select date_format(order_date,'%M')
, sum(order_total_amount)
from invoice_order
where user_id = '$user_id'
group
by year(order_date)
, month(order_date)
order
by year(order_date)
, month(order_date)
";
The above query would return January and February along with their respective order amount. But what I'm trying to do is to return all the month's names with the value of 0 if data does not exist in the table.
@bj?�rn-b??ttner this is for you. Could you please tell me what am I doing wrong?
$sale_months = array("January"=>0,"February"=>0,"March"=>0,"April"=>0,"May"=>0,"June"=>0,"July"=>0,"August"=>0,"September"=>0,"October"=>0,"November"=>0,"December"=>0);
$sale = "select date_format(order_date,'%M'), sum(order_total_amount) from invoice_order where user_id='$user_id' group by year(order_date),month(order_date) order by year(order_date),month(order_date)";
$sale_query = mysqli_query($connection,$sale);
$sale_result = mysqli_fetch_assoc($sale_query);
foreach($sale_result as $row) {
$sale_months[$row[0]] = $sale_months[$row[1]];
}
print_r($sale_result); This would return the Array ( [date_format(order_date,'%M')] => January [sum(order_total_amount)] => 40000 ) 1
why not usedate()
on php-side to create an array with 0 prefilled and then just filling the months with something else that are returned from the database? Or just have the months hardcoded there?
$months= array("January"=>0,"February"=>0);
foreach($query->fetchAll() as $row) {
$months[$row[0]] = $months[$row[1]];
}
Credit:- Select query to results all 12 months even if data not exist
Worked perfectly
SELECT
SUM(IF(month = 'Jan', total, 0)) AS 'Jan',
SUM(IF(month = 'Feb', total, 0)) AS 'Feb',
SUM(IF(month = 'Mar', total, 0)) AS 'Mar',
SUM(IF(month = 'Apr', total, 0)) AS 'Apr',
SUM(IF(month = 'May', total, 0)) AS 'May',
SUM(IF(month = 'Jun', total, 0)) AS 'Jun',
SUM(IF(month = 'Jul', total, 0)) AS 'Jul',
SUM(IF(month = 'Aug', total, 0)) AS 'Aug',
SUM(IF(month = 'Sep', total, 0)) AS 'Sep',
SUM(IF(month = 'Oct', total, 0)) AS 'Oct',
SUM(IF(month = 'Nov', total, 0)) AS 'Nov',
SUM(IF(month = 'Dec', total, 0)) AS 'Dec'
FROM
(SELECT
MIN(DATE_FORMAT(order_date, '%b')) AS month,
SUM(order_total_amount) AS total
FROM
invoice_order
WHERE
user_id = '1'
GROUP BY YEAR(order_date) , MONTH(order_date)
ORDER BY YEAR(order_date) , MONTH(order_date)) AS sale
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.