Home > front end >  MySQL query GROUP BY day/month/year
MySQL query GROUP BY day/month/year

Time:10-21

Is it possible to make a simple query to count the number of records I have in a specified time period like a year, month or day, having a field, like: public function group(){

$em = $this->getEntityManager();
$sub = $em->createQueryBuilder();

$qb = $sub;
$qb->select('DAY(s.start_date), YEAR(s.start_date), MONTH(s.start_date), COUNT(*)')
    ->from('App\Entity\Solts', 's')
    ->groupBy("DAY(s.start_date), YEAR(s.start_date), MONTH(s.start_date)" );
         
$query = $sub -> getQuery();

return $query -> getResult();

}

I want him to show me like this the result : => enter image description here

CodePudding user response:

Based on this response https://stackoverflow.com/a/48047792/3866856 you can try:

return $qb =   $this->createQueryBuilder('s')
                ->select('DATE_FORMAT(s.start_date, \'%Y-%m-%d\') as yearMonthDay, DATE_FORMAT(e.start_date, \'%Y\') as d_year, DATE_FORMAT(e.start_date, \'%m\') as d_month, DATE_FORMAT(e.start_date, \'%d\') as d_day,  count(s.id) countItems')
                ->groupBy('yearMonthDay')
                ->getQuery()
                ->getResult();

CodePudding user response:

I have my table slots to create time slots and I want to group the time by date to give me a display as it is possible

enter image description here

and the tables enter image description here

  • Related