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 <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to