A
A
Andrew2015-11-19 21:21:30
PHP
Andrew, 2015-11-19 21:21:30

How to display data from mysql grouped by month?

Hello, I have a table with the following structure

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `price` varchar(10) DEFAULT NULL,
  `date` int(8) DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

The date field stores a timestamp. It is necessary to somehow pull out the data and group it by year and month. I want to make an analytical table from this data, but I don’t understand how. That is, something like this:
2013
Январь
-запись
-запись
-запись
Февраль
-запись
-запись
-запись
......
2014
Январь
-запись
-запись
-запись
Февраль
-запись
-запись
-запись

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2015-11-20
@ntzch

In general, the grouping problem was in the timestamp date, there are two options either to make the field in the database not int, but date or leave int and use FROM_UNIXTIME (unix_timestamp) in the request. The
request, approximately, in my case, looks like this
Here there is a selection by years, then in a loop in a similar query we display months and records for them

I
IceJOKER, 2015-11-19
@IceJOKER

....order by `date`
further in the code:

$last_month = '';

START_LOOP
  if($last_month != $row['month']) { 
    $last_month = $row['month'];
    echo $row['month'];
  }
END_LOOP
//вот вам принцип работы, дальше сами

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question