Get id, distinct idLocal of the MIN(datetime) in MySQL I use MySQL 5.7 and I have a table with for example this data:

   id   idLocal       time
    1     1      2016-12-15 19:18:00    
    2     1      2016-12-15 19:18:00 
    3     2      2016-12-15 20:30:00 
    4     3      2016-12-16 20:30:00 
    5     2      2016-12-15 12:15:00 
    6     4      2016-12-15 12:15:00 


I need extract id, distinct idLocal of the MIN(time). The sentence must return this:

   id   idLocal       time
    1     1      2016-12-15 19:18:00    
    4     3      2016-12-16 20:30:00 
    5     2      2016-12-15 12:15:00 
    6     4      2016-12-15 12:15:00 


If idLocal have two rows with the same time, only need the first row.

I try with this sentence but if idLocal have two rows with the same time return me the two rows.

SELECT  P.id, fP.idLocal  ,P.time FROM Products P JOIN (SELECT idLocal,MIN(time) as time FROM Products GROUP BY idLocal) fP ON P.time = fP.time AND P.idLocal = fP.idLocal;


How do I solve it?
Make your query a subquery.

Take the result of the subquery and find minimum id grouped by idLocal,time ordered by id

PROPOSED QUERY

SELECT MIN(id) as id,idLocal,time
FROM (SELECT P.id,fP.idLocal,P.time
FROM Products P JOIN
(SELECT idLocal,MIN(time) as time FROM Products GROUP BY idLocal) fP
ON P.time = fP.time AND P.idLocal = fP.idLocal) A
GROUP BY idLocal,time
ORDER BY id;


YOUR SAMPLE DATA

DROP DATABASE IF EXISTS carlos;
CREATE DATABASE carlos;
USE carlos
CREATE TABLE Products
(
    id      INT NOT NULL AUTO_INCREMENT,
    idLocal INT NOT NULL,
    time    DATETIME NOT NULL,
    PRIMARY KEY (id)
);
INSERT INTO Products (idLocal,time) VALUES
(1,'2016-12-15 19:18:00'),(1,'2016-12-15 19:18:00'),
(2,'2016-12-15 20:30:00'),(3,'2016-12-16 20:30:00'),
(2,'2016-12-15 12:15:00'),(4,'2016-12-15 12:15:00');
SELECT * FROM Products;


YOUR SAMPLE DATA LOADED

mysql&gt; DROP DATABASE IF EXISTS carlos;
Query OK, 1 row affected (0.01 sec)

mysql&gt; CREATE DATABASE carlos;
Query OK, 1 row affected (0.00 sec)

mysql&gt; USE carlos
Database changed
mysql&gt; CREATE TABLE Products
    -&gt; (
    -&gt;     id      INT NOT NULL AUTO_INCREMENT,
    -&gt;     idLocal INT NOT NULL,
    -&gt;     time    DATETIME NOT NULL,
    -&gt;     PRIMARY KEY (id)
    -&gt; );
Query OK, 0 rows affected (0.03 sec)

mysql&gt; INSERT INTO Products (idLocal,time) VALUES
    -&gt; (1,'2016-12-15 19:18:00'),(1,'2016-12-15 19:18:00'),
    -&gt; (2,'2016-12-15 20:30:00'),(3,'2016-12-16 20:30:00'),
    -&gt; (2,'2016-12-15 12:15:00'),(4,'2016-12-15 12:15:00');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql&gt; SELECT * FROM Products;
+----+---------+---------------------+
| id | idLocal | time                |
+----+---------+---------------------+
|  1 |       1 | 2016-12-15 19:18:00 |
|  2 |       1 | 2016-12-15 19:18:00 |
|  3 |       2 | 2016-12-15 20:30:00 |
|  4 |       3 | 2016-12-16 20:30:00 |
|  5 |       2 | 2016-12-15 12:15:00 |
|  6 |       4 | 2016-12-15 12:15:00 |
+----+---------+---------------------+
6 rows in set (0.01 sec)


PROPOSED QUERY EXECUTED

mysql&gt; SELECT MIN(id) as id,idLocal,time
    -&gt; FROM (SELECT P.id,fP.idLocal,P.time
    -&gt; FROM Products P JOIN
    -&gt; (SELECT idLocal,MIN(time) as time FROM Products GROUP BY idLocal) fP
    -&gt; ON P.time = fP.time AND P.idLocal = fP.idLocal) A
    -&gt; GROUP BY idLocal,time
    -&gt; ORDER BY id;
+------+---------+---------------------+
| id   | idLocal | time                |
+------+---------+---------------------+
|    1 |       1 | 2016-12-15 19:18:00 |
|    4 |       3 | 2016-12-16 20:30:00 |
|    5 |       2 | 2016-12-15 12:15:00 |
|    6 |       4 | 2016-12-15 12:15:00 |
+------+---------+---------------------+
4 rows in set (0.00 sec)

mysql&gt;


GIVE IT A TRY !!!