Page 1 sur 1

GROUP BY et ORDER BY

Posté : 21 sept. 2007, 11:30
par Rei Itchido
Bonjour à tous

Petit cas concret MySQL tout bête (enfin je croyais à la base :) )

J'ai une table avec 3 champs id (autoincrementé), champs1 et champs2
J'ai comme enregistrements :
1, aaa, toto
2, aaa, toto
Et je voudrais recuperer l'id le plus grand pour les couples champs1-champs2
Je pensais que la requête suivante me renverrait la bonne info mais je récupère 1 et non 2

Code : Tout sélectionner

SELECT id FROM table GROUP BY champs1,champs2 ORDER BY id DESC
Merci d'avance de votre aide

Posté : 21 sept. 2007, 11:36
par Ryle
Et pourquoi ne pas utiliser la fonction MAX() du langage SQL ? :)

Code : Tout sélectionner

SELECT champs1, champs2, MAX(id) AS id_max FROM table GROUP BY champs1, champs2

Posté : 21 sept. 2007, 11:39
par Rei Itchido
Car je n'y avais pas pensé? :p

Merci