Return Date data according to month

Asked

Viewed 83 times

0

I developed the following function:

public function get_agendas()
{
    $this->db->select("*, EXTRACT(MONTH FROM data) as mes_referencia, count(id) as total_registros");
    $this->db->where("data >=", date("Y-m-d"));
    $this->db->group_by("EXTRACT(MONTH FROM data)");
    $this->db->order_by('id', 'ASC');
    $consulta = $this->db->get('agenda')->result();

        foreach($consulta as $valor){
            $valor->lista_agenda = $this->db->get('agenda')->result();
        }

    return $consulta;
}

My question is the following: At the time to list all the records belonging to that month, as I would SQL?

I have 4 records in the database: inserir a descrição da imagem aqui

I need to display it this way:

Month 04 Record 1 Record 2

Month 05 Record 1

Month 06 Record 1

The first part, I already know that will search how many records and I can even display the month correctly, but in the second part of the code within foreach(), I do not know how to do. Thanks!

  • You want to display the amount of records per month or want to display the records grouped month by month?

1 answer

0


Your question is not clear, so I will answer for what I understand.

When you want to know the amount of data for a specific Month

select count(1) from horarioverificacao where EXTRACT(MONTH FROM data)=3

Below if you want to know the total for months

select  EXTRACT(MONTH FROM data) ,count(1) from horarioverificacao 
group by EXTRACT(MONTH FROM data)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.