Bug #9990 | CHANGE IN BEHAVIOR OF GROUP BY WORKING BY PARSER | ||
---|---|---|---|
Submitted: | 19 Apr 2005 5:24 | Modified: | 25 Sep 2007 10:14 |
Reporter: | AJIT DIXIT | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Command-line Clients | Severity: | S1 (Critical) |
Version: | 5.0.4 | OS: | Linux (Linux) |
Assigned to: | Geert Vanderkelen | CPU Architecture: | Any |
[19 Apr 2005 5:24]
AJIT DIXIT
[28 Apr 2005 8:14]
Geert Vanderkelen
Hi Ajit, Could you please send us additional data so we could reproduce this? The CREATE TABLE of the tables involved and if possible data? Thanks, Geert
[28 May 2005 23:00]
Bugs System
No feedback was provided for this bug for over a month, so it is being suspended automatically. If you are able to provide the information that was originally requested, please do so and change the status of the bug back to "Open".
[24 Sep 2007 21:51]
Gerard Sychay
I experienced the same problem. I am on: % mysql -V mysql Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (x86_64) using readline 5.0 To reproduce: 1) Create table: CREATE TABLE `temp` ( `person_id` int(10) NOT NULL, `group_id` int(10) default NULL ); insert into temp values (1, 1), (2, null), (3, 1), (4, 4), (5, 4), (6, null); 2) Run query: select person_id, case when group_id is null then person_id else group_id end as group_id from temp group by group_id order by person_id asc 3) In mysql 4, it returns: +-----------+----------+ | person_id | group_id | +-----------+----------+ | 1 | 1 | | 2 | 2 | | 4 | 4 | | 6 | 6 | +-----------+----------+ In mysql 5, it returns: +-----------+----------+ | person_id | group_id | +-----------+----------+ | 1 | 1 | | 2 | 2 | | 4 | 4 | +-----------+----------+ It looks like this is because GROUP BY is grouping by the actual column, and not the alias name, as described by the original submitter. FYI, the business case for this query might be: I have persons who may or may not be in a group. Give me all persons, but combine people in a group by returning only the first person in that group.
[25 Sep 2007 10:14]
Geert Vanderkelen
Thanks for the test case Gerard. My dear colleague Sveta had to hit me with a clue bat :) The result is more correct in 5.0. MySQL is grouping by the column instead of the 'alias'. You can see that using SHOW WARNINGS. Best is to use a different name for the alias.