I have a little table where i like to get the latest content for each groupId.
This is the Table with my data:
This is my try to get it.
SELECT *, MAX(highest_datetime.creattionDate)
FROM highest_datetime
GROUP BY highest_datetime.groupId;
The MAX(highest_datetime.creattionDate) includes the latest content witch is a success.
But the complete rest of the line does not match the MAX(highest_datetime.creattionDate).
The rest of the row got mixed up.
How do i prevent this?
Do i use it wrong?
here is the data for the Database so you can create it yourself:
CREATE TABLE `test`.`highest_datetime` (
`id` INT NOT NULL AUTO_INCREMENT,
`groupId` INT NOT NULL,
`creationDate` DATETIME NOT NULL,
`Content` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`groupId`)
) ENGINE = InnoDB;
INSERT INTO `test`.`highest_datetime` (`groupId`, `creationDate`, `Content`)
VALUES
(1, '2023-05-02 00:00:00', 'Newest content'),
(1, '2023-01-01 00:00:00', 'Middle Content'),
(2, '2023-03-01 00:00:00', 'Middle Content'),
(2, '2022-11-01 00:00:00', 'Old Content'),
(2, '2023-06-01 00:00:00', 'New Content'),
(1, '2022-11-20 00:00:00', 'Old Content'),
(2, '2024-11-01 00:00:00', 'Future Content');