
- 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
How to group by column name and ensure the query retrieves the last update in MySQL?
Let us first create a table −
mysql> create table DemoTable621 (UserName varchar(100),UserEmailId varchar(100),UserLastPost datetime); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable621 values('John','John@gmail.com','2019-04-10 11:01:10'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable621 values('John','John@gmail.com','2019-07-14 13:07:10'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable621;
This will produce the following output −
+----------+----------------+---------------------+ | UserName | UserEmailId | UserLastPost | +----------+----------------+---------------------+ | John | John@gmail.com | 2019-04-10 11:01:10 | | John | John@gmail.com | 2019-07-14 13:07:10 | +----------+----------------+---------------------+ 2 rows in set (0.00 sec)
Following is the query to group by column name and ensure the query retrieves the last update −
mysql> select UserName,UserEmailId,max(UserLastPost) from DemoTable621 group by UserName,UserEmailId;
This will produce the following output −
+----------+----------------+---------------------+ | UserName | UserEmailId | max(UserLastPost) | +----------+----------------+---------------------+ | John | John@gmail.com | 2019-07-14 13:07:10 | +----------+----------------+---------------------+ 1 row in set (0.19 sec)
- Related Articles
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to group by names and display the count in a new column
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- Group concatenate the last name from a MySQL column and set a condition to display limited records
- MySQL query to display columns name first name, last name as full name in a single column?
- Add a column count in a MySQL query on the basis of last name records?
- MySQL query to search within the last 5 characters in a column?
- MySQL: update field with Group By?
- MySQL query to conduct a basic search for a specific last name in a column
- MySQL query to select rows where column value is only 0, group by another column?
- Update the table by calculating the sum and display the result as last column value
- MySQL query to get the last created table name (most recent)?
- MySQL query to select everything to left of last space in a column with name records
- MySQL query to remove everything except the last 7 characters in column record
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers

Advertisements