Group results with current date

1

Example, I have a query mysql like this

SELECT * FROM post WHERE fecha >= $time;

where $time = current week

I also have a array with the days, which are grouped

$days = array('Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado', 'Domingo')

How could I group the results with php according to their current day?

Example:

All posts on Monday

-Post del dia lunes 1
-Post del dia lunes 2

All posts of the day Tuesday

-Post del dia martes 1
-Post del dia martes 2

Wednesday, Thursday, Friday, Saturday Sunday ....

psd: Group the results, according to your day with php

    
asked by Jadel 26.06.2017 в 16:14
source

2 answers

3

Maybe this can solve your problem:

    SELECT GROUP_CONCAT(nombre SEPARATOR '|') as nombres, fecha
    FROM   test_semana
    WHERE  YEARWEEK('fecha', 1) = YEARWEEK(CURDATE(), 1)
    GROUP BY DAY(fecha)
    ORDER BY fecha;
  • GROUP_CONCAT along with GROUP BY DAY(fecha) create an array of the records for each day in a column called nombres .
  • You can use explode to read the value of each record using the | separator
  • WHERE YEARWEEK( date , 1) = YEARWEEK(CURDATE(), 1) gets automatically the records for the current week.

Here you can see the doc on YEARWEEK . And on GROUP_CONCAT .

Ejemplo completo: ver demo

CREATE TABLE IF NOT EXISTS test_semana (
         id SERIAL,
         nombre VARCHAR(50),
         fecha date
       );

 INSERT INTO test_semana (fecha, nombre) VALUES
     ('2017-04-01', 'Pedro'),
     ('2017-04-02', 'Santiago'),
     ('2017-04-03', 'Juan'),
     ('2017-06-25', 'Andrés'),
     ('2017-06-26', 'Felipe'),
     ('2017-06-27', 'Mateo'),
     ('2017-06-27', 'Marcos'),
     ('2017-06-28', 'Ana'),
     ('2017-06-28', 'María'),
     ('2017-06-28', 'José'),
    ('2017-07-20', 'Pablo');



SELECT * FROM test_semana;  


SELECT GROUP_CONCAT(nombre SEPARATOR '|') as nombres, fecha
FROM   test_semana
WHERE  YEARWEEK('fecha', 1) = YEARWEEK(CURDATE(), 1)
GROUP BY DAY(fecha)
ORDER BY fecha;

Resultado

All records:

    id  nombre      fecha
1   1   Pedro       01.04.2017 00:00:00
2   2   Santiago    02.04.2017 00:00:00
3   3   Juan        03.04.2017 00:00:00
4   4   Andrés      25.06.2017 00:00:00
5   5   Felipe      26.06.2017 00:00:00
6   6   Mateo       27.06.2017 00:00:00
7   7   Marcos      27.06.2017 00:00:00
8   8   Ana         28.06.2017 00:00:00
9   9   María       28.06.2017 00:00:00
10  10  José        28.06.2017 00:00:00
11  11  Pablo       20.07.2017 00:00:00

Filtered records: this is what matters

    nombres         fecha
1   Felipe          26.06.2017 00:00:00
2   Mateo|Marcos    27.06.2017 00:00:00
3   Ana|María|José  28.06.2017 00:00:00
    
answered by 26.06.2017 в 17:06
0

To get the post ordered by date and day of the week:

SELECT *, WEEKDAY($fecha) AS DAY
FROM post WHERE fecha <= $time
ORDER BY $fecha, WEEKDAY($fecha);

If you want to group them you can do a GROUP BY this same criterion or use it in WHERE to define a specific day / s.

DAY's values are days 0 to 6 starting on Monday.

    
answered by 26.06.2017 в 16:33