A tester en alternative de phpmyadmin ⇒ http://www.chive-project.com/
id | user_id | date | name |
---|---|---|---|
1 | 1 | 2020-03-10 | toto |
2 | 1 | 2020-03-20 | super toto |
3 | 2 | 2020-01-01 | tata |
je veux les derniers nom des utilisateurs
SELECT user_id, name FROM users t1 WHERE EXISTS(SELECT 1 FROM users t2 WHERE t2.user_id = t1.user_id GROUP BY t2.user_id HAVING t1.date= MAX(t2.date))
user_id | name |
---|---|
1 | super toto |
2 | tata |
Par contre, si vous avez des date dans le futur, il faudra utiliser cette requête
SELECT user_id, name FROM users t1 WHERE EXISTS(SELECT 1 FROM users t2 WHERE t2.user_id = t1.user_id AND t2.date<=now() GROUP BY t2.user_id HAVING t1.date= MAX(t2.date))
Variantes de la 1ère requête qui semble plus rapide
SELECT t1.user_id,t1.name FROM users AS t1, ( SELECT user_id, MAX(DATE) AS DATE FROM users GROUP BY user_id ) AS t2 WHERE t1.user_id=t2.user_id AND t1.date=t2.date