Also, note that MySQL has a bug in its group by where it will let you select a column that is NOT grouped without using an aggregate function on it, which is illegal, according to the SQL spec. This leads to unpredictable results:
If I create a table in both MySQL and Postgresql like so:
create table test (a int, b int, c int);
insert into test values (1,1,1);
insert into test values (1,1,2);
insert into test values (1,1,3);
insert into test values (1,1,4);
insert into test values (1,2,1);
insert into test values (1,2,2);
insert into test values (1,2,3);
insert into test values (2,1,1);
insert into test values (2,1,2);
insert into test values (2,1,3);
insert into test values (2,1,4);
And then run the query:
select * from test group by a;
I get this from postgresql:
ERROR: attribute "test.b" must be GROUPed or used in an aggregate function
and rightly so. After all, which number from the second column do I want? Postgresql isn't gonna guess for me, and the SQL spec says to throw an error. In MySQL, I get this:
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
+------+------+------+
2 rows in set (0.00 sec)
I.e. it just chose the first value it could find. The get the equivalent in a SQL spec database, you need this:
select a,max(b),max(c) from test group by a;
a | max | max
---+-----+-----
2 | 1 | 4
1 | 2 | 4
Or replace the max with min, etc...
My point being, and I have one, is that if you aren't grouping the field, then you're gonna just get whichever random row MySQL happens to grab, and your results may not be reproduceable. so, you need to add a max() or min() to your query if you aren't going to group by it.