1
Suppose the following table records authors' posts on a blog.
An author may have several posts.
So I have several repetitions of authors in this table of the different posts that he did.
How I should query to find the content of the most recent post according to the data_post column.
For example, the author of id_author = 1 his most recent post is from 25-08-2018 with the content science.
I did the following search but is returning to me the political content instead of science:
Result with group by: Consultation:
SELECT id_post,id_autor,MAX(data_post),conteudo_post FROM teste group by id_autor order by data_post DESC;
If you want to replicate the example follow create and I played jsfidle json if you want to import the data. https://jsfiddle.net/0ofe976w/1/
CREATE TABLE `teste` (
`id_post` int(11) NOT NULL,
`id_autor` int(11) NOT NULL,
`data_post` date DEFAULT NULL,
`conteudo_post` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id_post`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Perfect...I understood...I think it’s funny that Mysql lets you select columns that are not with an aggregate function like MAX or are not in group by. What kind of function does it use to choose which data to display other than this aggregate in group by or that is not contained in an aggregation function? For example this query: SELECT id_author, MAX(data_post) AS MAX_DATA,content_post FROM test GROUP BY id_author ?
– L.J