Page 1 sur 1

Recherche id max en plus d'un autre champs

Posté : 11 oct. 2005, 15:10
par mazou
Bonjour,

Je bloque sur une recherche dans des tables mysql.

Ma recherche n'avait pour l'instant rien de compliqué, il s'agissait juste de croiser des tables.

Seulement maintenant je veux trouver en plus l'enregistrement max d'un champ.

Voici ma requête :
$query="SELECT ibf_topics.title,  ibf_topics.tid, ibf_posts.post, ibf_posts.pid, ibf_posts.post_date FROM ibf_topics, ibf_posts where ibf_topics.tid=ibf_posts.topic_id  order by  ibf_topics.tid desc";
Je voudrais qu'en plus que de cette recherche elle ne me donne que l'enregistrement avec le chiffre le plus important dans le champs pid (ibf_posts.pid).

QQ'un as-t-il une solution ???

Merci d'avance

Posté : 11 oct. 2005, 18:35
par Truc
SAlut, en 2 requetes ?!
une pour trouver le max et une autre (la meme que tu as) + clause where sur ibf_posts.pid=Max(trouvé) :-k

Posté : 12 oct. 2005, 09:44
par mazou
SAlut, en 2 requetes ?!
une pour trouver le max et une autre (la meme que tu as) + clause where sur ibf_posts.pid=Max(trouvé) :-k
Oui en 2 requête je sais le faire (c d'ailleurs ce que j'ai fait).
Mais j'aurai aimé le faire en une

Posté : 14 oct. 2005, 10:41
par Augure
Essaye comme cela :
$query="
SELECT ibf_topics.title,  ibf_topics.tid, ibf_posts.post, ibf_posts.pid, ibf_posts.post_date 
FROM ibf_topics, ibf_posts 
WHERE ibf_topics.tid=ibf_posts.topic_id
GROUP BY ibf_posts.pid
HAVING ibf_posts.pid = max(ibf_posts.pid)";

Posté : 14 oct. 2005, 10:51
par mere-teresa
J'ai cru comprendre que tu voulais tes champs et aussi le plus grand id...(MAX)...donc mets le comme un des champs que tu sélectionne et renomme le avec AS.

Code : Tout sélectionner

SELECT SELECT MAX(ibf_posts.pid) AS maximum, ibf_topics.title, ibf_topics.tid, ibf_posts.post, ibf_posts.pid, ibf_posts.post_date FROM ibf_topics, ibf_posts WHERE ibf_topics.tid=ibf_posts.topic_id ORDER BY ibf_topics.tid desc
http://www.nexen.net/docs/mysql/annotee ... p?lien=max

Posté : 18 oct. 2005, 09:25
par mazou
Essaye comme cela :
$query="
SELECT ibf_topics.title,  ibf_topics.tid, ibf_posts.post, ibf_posts.pid, ibf_posts.post_date 
FROM ibf_topics, ibf_posts 
WHERE ibf_topics.tid=ibf_posts.topic_id
GROUP BY ibf_posts.pid
HAVING ibf_posts.pid = max(ibf_posts.pid)";
Cette solution fonctionne !

Merci à tous