List:General Discussion« Previous MessageNext Message »
From:Gerald Clark Date:April 4 2002 7:11pm
Subject:Re: Group by?
View as plain text  
Maybe you really wanted ORDER BY.

Tobias K. Tobiasen wrote:

> Hello
>
> I think the gruop by is behaving strange. Could someone please explain 
> this or maybe point me to a definition of the group by. (Inspired by 
> http://www.mysql.org/doc/E/x/Examples.html).
>
>
> CREATE TABLE shop (
>  article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
>  dealer  CHAR(20)                 DEFAULT ''     NOT NULL,
>  price   DOUBLE(16,2)             DEFAULT '0.00' NOT NULL,
>  PRIMARY KEY(article, dealer));
>
> INSERT INTO shop VALUES
> (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69),
> (3,'D',1.25),(4,'D',19.95);
>
> mysql> SELECT * FROM shop;
>
> +---------+--------+-------+
> | article | dealer | price |
> +---------+--------+-------+
> |    0001 | A      |  3.45 |
> |    0001 | B      |  3.99 |
> |    0002 | A      | 10.99 |
> |    0003 | B      |  1.45 |
> |    0003 | C      |  1.69 |
> |    0003 | D      |  1.25 |
> |    0004 | D      | 19.95 |
> +---------+--------+-------+
> 7 rows in set (0.00 sec)
>
> mysql> select * from shop GROUP BY article;
> +---------+--------+-------+
> | article | dealer | price |
> +---------+--------+-------+
> |    0001 | A      |  3.45 |
> |    0002 | A      | 10.99 |
> |    0003 | B      |  1.45 |
> |    0004 | D      | 19.95 |
> +---------+--------+-------+
> 4 rows in set (0.00 sec)
>
> QUESTION: I had expected 7 rows - where did the rest go?
>
>
> mysql> select * from shop GROUP BY article having price=max(price);
> +---------+--------+-------+
> | article | dealer | price |
> +---------+--------+-------+
> |    0002 | A      | 10.99 |
> |    0004 | D      | 19.95 |
> +---------+--------+-------+
> 2 rows in set (0.00 sec)
>
> QUESTION: I had expected 4 rows (one for each article) - where did the 
> rest go?
>
>
> mysql> select * from shop GROUP BY article having price=min(price);
> +---------+--------+-------+
> | article | dealer | price |
> +---------+--------+-------+
> |    0001 | A      |  3.45 |
> |    0002 | A      | 10.99 |
> |    0004 | D      | 19.95 |
> +---------+--------+-------+
> 3 rows in set (0.00 sec)
>
> QUESTION: It seems like group by only return the first row in the 
> group - is this correct?
>
> I am not trying to solve a specifik problem - I would just like to 
> know how the group by works in MySQL.
>
> T2K
>
>
>
> ---------------------------------------------------------------------
> Before posting, please check:
>   http://www.mysql.com/manual.php   (the manual)
>   http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <mysql-thread105122@stripped>
> To unsubscribe, e-mail 
> <mysql-unsubscribe-gerald_clark=suppliersystems.com@stripped>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>
>


Thread
Group by?Tobias K. Tobiasen4 Apr
  • Re: Group by?Gerald Clark4 Apr
  • Re: Group by?Fournier Jocelyn [Presence-PC]4 Apr
  • Re: Group by?Harald Fuchs5 Apr
  • Group by?Michael Widenius5 Apr