I have the following SQL structure:
CREATE TABLE IF NOT EXISTS 'noticia' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'id_categoria' int(11) DEFAULT NULL,
'titulo' varchar(255) NOT NULL,
'previa' text NOT NULL,
'descricao' text,
'imagem' varchar(255) NOT NULL DEFAULT '',
'data' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
'views' int(11) DEFAULT '0',
'youtube' varchar(255) DEFAULT NULL,
'ativo' tinyint(4) NOT NULL,
PRIMARY KEY ('id','ativo')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I would like to create a SQL that will query the records, grouping month and year. that appears as follows:
I thought about doing:
SELECT * FROM noticia GROUP BY MONTH(data);
If I do this way, you will group only by month, and if you have next month the same month, you will also group. And that is not the intention. Thanks!