Query in MYSQL DATETIME ()

5

I have a database in which the packages of some clients are stored and the date of creation of said package.

I need to check out how many packages a client makes each day in a month. I've tried with

SELECT distinct fecha_creacion FROM paqueteria.paquetes
where remitente=73
and date(fecha_creacion) >= '2017-10-01'
and date(fecha_creacion) <='2017-10-30'

The problem is that date_creation is in a datetime format YYYY-MM-DD HH-MM-SS and the same day I get several records but with different time

2017-10-02 13:57:34
2017-10-02 13:58:15
2017-10-03 14:58:32
    
asked by hayber 25.10.2017 в 11:31
source

4 answers

6

What you need is to make use of the function DATE_FORMAT :

SELECT
  DISTINCT DATE_FORMAT(
    fecha_creacion,
    "%Y-%m-%d"
  ) fecha_creacion
FROM paquetes
WHERE
  remitente=73
AND
  fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'

Or the DATE function :

SELECT
  DISTINCT DATE(fecha_creacion) fecha_creacion
FROM paquetes
WHERE
  remitente=73
AND
  fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'

You can check its operation in this online version ( with DATE_FORMAT and with DATE ).

In both cases I used the alias fecha_creacion so that the name of the field remains the same although its content is only year, month and day.

If you want me to tell you the number of packages then you should use GROUP BY instead of DISTINCT :

SELECT
  DATE(fecha_creacion) fecha_creacion,
  COUNT(*) numero
FROM paquetes
WHERE
  fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'
GROUP BY DATE(fecha_creacion)

If you want to avoid the DATE(fecha_creacion) then you must change the alias to avoid confusion with the name of the field (which has preference):

SELECT
  DATE(fecha_creacion) fecha,
  COUNT(*) numero
FROM paquetes
WHERE
  fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'
GROUP BY fecha

Please note that the query is not optimized because neither fecha ni DATE(fecha_creacion) are indexes .

    
answered by 25.10.2017 / 11:44
source
3

I would simply do it like this:

SELECT COUNT(*) 
    total, remitente , 
    DATE(fecha_creacion) fecha 
FROM paquetes_20171025 
WHERE remitente=73 AND fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'
GROUP BY fecha;

Proof of concept

VIEW DEMO

CREATE TABLE IF NOT EXISTS paquetes_20171025 
(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    remitente INT,  fecha_creacion DATETIME
)ENGINE=INNODB;


INSERT INTO paquetes_20171025 (remitente, fecha_creacion)
    VALUES 
    (73,'2016-10-02 13:57:34'),
    (73,'2016-10-02 13:58:15'),
    (73,'2016-10-03 14:58:32'),
    (73,'2017-10-02 13:57:34'),
    (73,'2017-10-02 13:58:15'),
    (73,'2017-10-03 14:58:32'),
    (73,'2017-10-04 13:57:34'),
    (73,'2017-10-04 13:58:15'),
    (73,'2017-10-05 14:58:32'),
    (74,'2017-10-02 13:57:34'),
    (74,'2017-10-02 13:58:15'),
    (75,'2017-10-03 14:58:32'),
    (76,'2017-10-04 13:57:34'),
    (77,'2017-10-04 13:58:15'),
    (77,'2017-10-05 14:58:32')
;

-- Datos de prueba 


SELECT COUNT(*) 
    total, remitente , 
    DATE(fecha_creacion) fecha 
FROM paquetes_20171025 
WHERE remitente=73 AND fecha_creacion BETWEEN '2017-10-01' AND '2017-10-31'
GROUP BY fecha;

Result:

total   remitente    fecha
2       73           02.10.2017 00:00:00
1       73           03.10.2017 00:00:00
2       73           04.10.2017 00:00:00
1       73           05.10.2017 00:00:00
    
answered by 25.10.2017 в 12:19
0

To show you the number of packages I would execute the following query:

SELECT
  DISTINCT DATE(fecha_creacion) as fecha_creacion, count(*) Paquetes
FROM paqueteria.paquetes
WHERE
  remitente=73
AND
  date(fecha_creacion) BETWEEN '2017-10-01' AND '2017-10-31'
group by fecha_creacion;

I hope it serves you. Greetings

    
answered by 25.10.2017 в 12:02
0

Friend convert the date with Cast like this:

SELECT fecha_creacion, remitente, count(*) FROM paqueteria.paquetes
where remitente=73
and Cast(fecha_creacion as Date) Between '2017-10-01'
and '2017-10-30'
Group by fecha_creacion
Order by 1

I did not try it, I wrote it and nothing else.

    
answered by 31.10.2017 в 05:04