WARNING! Date Format Problem
I strongly urge you to convert the CreadedOn column to a DATE data-type, instead of VARCHAR, in order to retrieve the appropriate ordering of values by date.
Otherwise 14-MAR-2020 will be considered later than 11-DEC-2020.
Issue Example DB-Fiddle
Schema
CREATE TABLE users (
`ID` INTEGER,
`Previous` VARCHAR(5),
`Current` VARCHAR(6),
`User` VARCHAR(18),
`createdOn` VARCHAR(20)
);
INSERT INTO users
(`ID`, `Previous`, `Current`, `User`, `createdOn`)
VALUES
('1', 'RED', 'BLUE', 'System', '14-MAR-2020'),
('2', 'GREEN', 'YELLOW', 'ADMIN', '12-MAR-2020'),
('3', 'GREEN', 'PURPLE', 'System', '11-DEC-2020'),
('4', 'GREEN', 'YELLOW', 'System', '10-MAR-2020');
Other answer query https://stackoverflow.com/a/61316029/1144627
select
Id,
Previous,
User,
CreatedOn,
(
select Current
from users
order by CreatedOn desc
limit 1
) as Current
from users
where `user` = 'ADMIN'
order by createdon desc
limit 1;
| Id | Previous | User | CreatedOn | Current |
| --- | -------- | ----- | ----------- | ------- |
| 2 | GREEN | ADMIN | 12-MAR-2020 | BLUE |
Expected Current of PURPLE
To fix the issue with the date sorting, you will need to modify your table using the STR_TO_DATE() function.
It is important to note that comparing with STR_TO_DATE in your query instead of updating the column will cause a full-table scan, comparing every record in the table.
Example DB-Fiddle
ALTER TABLE users
ADD COLUMN createdOn_Date DATE NULL DEFAULT NULL;
UPDATE users
SET CreatedOn_Date = STR_TO_DATE(CreatedOn, '%d-%b-%Y')
WHERE CreatedOn_Date IS NULL;
ALTER TABLE users
DROP COLUMN CreatedOn,
CHANGE COLUMN CreatedOn_Date CreatedOn DATE;
Then display your records in your desired format, use the DATE_FORMAT() function
Other Answer Query https://stackoverflow.com/a/61316029/1144627
select
Id,
Previous,
User,
DATE_FORMAT(CreatedOn, '%d-%b-%Y') AS CreatedOn,
(
select Current
from users
order by CreatedOn desc
limit 1
) as Current
from users
where `user` = 'ADMIN'
order by createdon desc
limit 1;
Result
| Id | Previous | User | CreatedOn | Current |
| --- | -------- | ----- | ----------- | ------- |
| 2 | GREEN | ADMIN | 12-Mar-2020 | PURPLE |