How To Get Hourly Data In MySQL

Websolutionstuff | Feb-04-2022 | Categories : MySQL

In this tutorial, we will see how to get hourly data in mysql. Many times we need to get hourly data or we are required to get data every hour in MySQL. For getting data for every hour in MySQL we use the hour() function. MySQL hour() returns the hour of a time. The return value is within the range of 0 to 23 for time-of-day values.

So, let's see SQL query to get data for every hour or MySQL query to get hour wise records in PHP and how to get hourly data in PHP from the database.

MySQL Example :

Using SQL query to get data for every hour in MySQL.

id | order_date          | amount
---+---------------------+-------
 1 | 2022-02-02 08:15:00 | 150
 2 | 2022-02-02 08:30:00 | 200
 3 | 2022-02-02 08:55:00 | 100
 4 | 2022-02-02 09:20:00 | 300
 5 | 2022-02-02 10:45:00 | 200
 7 | 2022-02-02 11:50:00 | 190

 In your table, the record needs to be like date and hour.

select hour(order_date), sum(amount)
       from users
       group by hour(order_date);

 

 

If you want to display hour values using AM/PM notation such as 10 AM, 11 AM, 12 PM, etc, then you need to use the date_format function that allows you to change the format of a given date and time.

select date_format(order_date,'%H %p') as hour,
        sum(amount) as total_amount
        from users
        group by date_format(order_date,'%H %p');

 


You might also like :

Recommended Post
Featured Post
How To Downgrade PHP 8 to 7.4 in Ubuntu
How To Downgrade PHP 8 to 7.4...

As a web developer, I understand the significance of embracing the latest technologies to stay ahead in the dynamic worl...

Read More

Aug-04-2023

How To Create Pie Chart In Laravel 9 Using Highcharts
How To Create Pie Chart In Lar...

In this article, we will see how to create a pie chart in laravel 9 using highcharts. A pie chart is a circular statisti...

Read More

Oct-05-2022

How To Setup And Configuration Angular 15
How To Setup And Configuration...

Setting up and configuring Angular 15, the latest version of the popular JavaScript framework, is a crucial step in star...

Read More

Jun-07-2023

How to Use JSON Data Field in MySQL Database
How to Use JSON Data Field in...

Today, In this post we will see how to use json field in mysql database. In this tutorial i will give mysql json data ty...

Read More

Jun-04-2021