Ankur,

Order of the GROUP BY will effect the sort order of the result and also
whether or not MySQL uses an index. If the order of the fields in the GROUP
BY differs from the index, it may not be utilised depending on your WHERE
clause...

Example of how the order is effected by GROUP BY:

You have a table, people, with firstname, surname and ph_num with a few
entries like..

firstname       surname           ph_num
Lachlan       Mulcahy             222 1234 1234
James         Mulcahy             222 1234 1234
Robert        Thompson            04 1673 1777


You may wish to:

SELECT
        surname, count(surname)
FROM
        people
GROUP BY
        surname, ph_num

This will give you the number of unique phone numbers per surname, ordered
by surname.

Changing the GROUP BY clause to be ph_num, surname will give you the same
data but sorted by ph_num.. in this case the Thompsons will come first
because 04 1673 1777 comes before 222 1234 1234 numerically.

Depending on your usage this sorting may be undesired.. This can be
overridden by your ORDER BY, however if you are conscious of your GROUP BY
ordering MySQL won't have to sort everything multiple times unnecessarily..

Check out:
http://dev.mysql.com/doc/mysql/en/ORDER_BY_optimization.html

Also Order of GROUP BY is sensitive if you're using ROLL UP as hierachy is
established..
http://dev.mysql.com/doc/mysql/en/GROUP-BY-Modifiers.html

HTH,
Lachlan






-----Original Message-----
From: ankur aggarwal [mailto:[EMAIL PROTECTED]
Sent: Wednesday, 11 August 2004 3:54 PM
To: [EMAIL PROTECTED]
Subject: Group by statement


hi all
i was tryin using mysql statement using the group by
statement..............................

can anyone tell me is ther any difference if i do

1. GROUP BY  A,B

2. GROUP BY  B,A

where A and B are any 2 attributes of a table..................

thanx for ur consideration
ankur


---------------------------------
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to