PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Using MAX function to fetch primary id
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using MAX function to fetch primary id

Réponse
 
LinkBack Outils de la discussion
Vieux 07/09/2007, 20h11   #1 (permalink)
Cathy Fusko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using MAX function to fetch primary id

Hi,
I need to select a max value for a group of records and I also need the
primary key for that record.

I am wondering if this can be done with a single query? e.g

Table_x

id count cat_id
1 10 1
2 20 2
3 35 2
4 15 1

with
SELECT id, cat_id, max(count)
FROM table_x
GROUP BY cat_id

I would probably get the following result

id cat_id max(count)
1 1 15
2 2 35
and what I would like to get is

id cat_id max(count)
4 1 15
3 2 35

Is there a way to do this with single query ?

cathy
www.nachofoto.com

  Réponse avec citation
Vieux 07/09/2007, 20h23   #2 (permalink)
Daevid Vincent
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Using MAX function to fetch primary id

> -----Original Message-----
> I need to select a max value for a group of records and I
> also need the
> primary key for that record.
>
> I am wondering if this can be done with a single query? e.g
>
> Table_x
>
> id count cat_id
> 1 10 1
> 2 20 2
> 3 35 2
> 4 15 1
>
> with
> SELECT id, cat_id, max(count)
> FROM table_x
> GROUP BY cat_id
>
> I would probably get the following result
>
> id cat_id max(count)
> 1 1 15
> 2 2 35
> and what I would like to get is
>
> id cat_id max(count)
> 4 1 15
> 3 2 35
>
> Is there a way to do this with single query ?


This is an old old topic and one that causes more grief than it's worth and
I wish mySQL would just "fix" this bug (IMHO it *is* a bug) so it works as
people EXPECT it to work...

Search the archives for these topics:

"Erroneus column using MAX() and GROUP BY"
and
" with subqueries... MAX() and GROUP BY"

Also these links may .

http://dev.mysql.com/doc/refman/4.1/...group-row.html

http://www.xaprb.com/blog/2006/12/07...astmax-row-per
-group-in-sql/

The short answer is "no", or you have to use sub-selects, which in effect is
not one select and equally slow and painful.

  Réponse avec citation
Vieux 11/09/2007, 03h02   #3 (permalink)
Brent Baisley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using MAX function to fetch primary id

You can do it as long as there is only a single record with the max
value. If there is more than 1 record with the same max value, there
isn't a single record to pull.

To do it, you would need to join on the results of the max query, and
part of the join condition would be the max value

SELECT id, count, cat_id FROM table_x JOIN
( SELECT cat_id, max(count) maxcount
FROM table_x
GROUP BY cat_id ) AS table_max
ON table_x.cat_id=table_max.cat_id AND table_x.count=table_max.maxcount

I'm not sure if I got the syntax perfect, but that the concept of how
you would do it.


On Sep 7, 2007, at 3:11 PM, Cathy Fusko wrote:

> Hi,
> I need to select a max value for a group of records and I also
> need the
> primary key for that record.
>
> I am wondering if this can be done with a single query? e.g
>
> Table_x
>
> id count cat_id
> 1 10 1
> 2 20 2
> 3 35 2
> 4 15 1
>
> with
> SELECT id, cat_id, max(count)
> FROM table_x
> GROUP BY cat_id
>
> I would probably get the following result
>
> id cat_id max(count)
> 1 1 15
> 2 2 35
> and what I would like to get is
>
> id cat_id max(count)
> 4 1 15
> 3 2 35
>
> Is there a way to do this with single query ?
>
> cathy
> www.nachofoto.com


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h49.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09783 seconds with 11 queries