
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to group data in the form of user login time per hour and get the records of the users logged in the recent hour?
You can use a subquery with JOIN condition for this. The syntax is as follows −
SELECT yourTablevariableName.* FROM ( SELECT MAX(UNIX_TIMESTAMP(yourDateTimeColumnName)) AS anyAliasName FROM getLatestHour GROUP BY HOUR(UserLoginDateTime) ) yourOuterVariableName JOIN yourTableName yourTablevariableName ON UNIX_TIMESTAMP(yourDateTimeColumnName) = yourOuterVariableName.yourAliasName WHERE DATE(yourDateTimeColumnName) = 'yourDateValue';
To understand the above syntax and the result to be achieved, let us create a table. The query to create a table is as follows −
mysql> create table getLatestHour -> ( -> UserId int, -> UserName varchar(20), -> UserLoginDateTime datetime -> ); Query OK, 0 rows affected (0.68 sec)
Now you can insert some records in the table using insert command. The records are the user records including the user login date and time. The query is as follows −
mysql> insert into getLatestHour values(100,'John','2019-02-04 10:55:51'); Query OK, 1 row affected (0.27 sec) mysql> insert into getLatestHour values(101,'Larry','2019-02-04 12:30:40'); Query OK, 1 row affected (0.16 sec) mysql> insert into getLatestHour values(102,'Carol','2019-02-04 12:40:46'); Query OK, 1 row affected (0.20 sec) mysql> insert into getLatestHour values(103,'David','2019-02-04 12:44:54'); Query OK, 1 row affected (0.17 sec) mysql> insert into getLatestHour values(104,'Bob','2019-02-04 12:47:59'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from getLatestHour;
The following is the output −
+--------+----------+---------------------+ | UserId | UserName | UserLoginDateTime | +--------+----------+---------------------+ | 100 | John | 2019-02-04 10:55:51 | | 101 | Larry | 2019-02-04 12:30:40 | | 102 | Carol | 2019-02-04 12:40:46 | | 103 | David | 2019-02-04 12:44:54 | | 104 | Bob | 2019-02-04 12:47:59 | +--------+----------+---------------------+ 5 rows in set (0.00 sec)
Here is the query to group data per hour and get the record of the recent hour. The query is as follows −
mysql> SELECT tbl1.* -> FROM ( -> SELECT MAX(UNIX_TIMESTAMP(UserLoginDateTime)) AS m1 -> FROM getLatestHour -> GROUP BY HOUR(UserLoginDateTime) -> ) var1 -> JOIN getLatestHour tbl1 -> ON UNIX_TIMESTAMP(UserLoginDateTime) = var1.m1 -> WHERE DATE(UserLoginDateTime) = '2019-02-04';
The following is the output −
+--------+----------+---------------------+ | UserId | UserName | UserLoginDateTime | +--------+----------+---------------------+ | 100 | John | 2019-02-04 10:55:51 | | 104 | Bob | 2019-02-04 12:47:59 | +--------+----------+---------------------+ 2 rows in set (0.05 sec)
- Related Articles
- MySQL Query to find tables modified in the last hour?
- MySQL Select to get users who have logged in today?
- List logged-in MySQL users?
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- How to update User Logged in Time for a specific user in MySQL?
- Find the ratio of 50 km per hour to 30 m per second.
- MongoDB Query to search for records only in a specific hour?
- MySQL query to get the last created table name (most recent)?
- MySQL query to get the count of distinct records in a column
- Python Pandas - Get the hour of the day component of the Period
- Python Pandas - Get the minute of the hour component of the Period
- How to convert 12-hour time scale to 24-hour time in R?
- Print the time an hour ago PHP?
- Python Pandas - Get the hour of the period from the PeriodIndex object
- Converting 12 hour format time to 24 hour format in JavaScript

Advertisements