Recherche id max en plus d'un autre champs

Eléphanteau du PHP | 49 Messages

11 oct. 2005, 15:10

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

Modérateur PHPfrance
Modérateur PHPfrance | 7636 Messages

11 oct. 2005, 18:35

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

/!\ Avant de poster se documenter et rechercher.
Qui ne sait pas rendre un service n'a pas le droit d'en demander.
MaBrute

Eléphanteau du PHP | 49 Messages

12 oct. 2005, 09:44

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

Eléphant du PHP | 91 Messages

14 oct. 2005, 10:41

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)";

Modérateur PHPfrance
Modérateur PHPfrance | 6037 Messages

14 oct. 2005, 10:51

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

Eléphanteau du PHP | 49 Messages

18 oct. 2005, 09:25

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