Suppose I have this table: named = the_table whose structure is:
postgreSQL:
create table the_table (col3 SERIAL, col2 varchar, col1 varchar, PRIMARY KEY(col3));
MySQL:
create table the_table ( col3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY, col2 varchar(20), col1 varchar(20) )
Then I inserted the table:
INSERT INTO the_table (col2,col1) VALUES
('x','a'),
('x','b'),
('y','c'),
('y','d'),
('z','e'),
('z','f');
Now the table looks like this:
col3 | col2 | col1
------+------+------
1 | x | a
2 | x | b
3 | y | c
4 | y | d
5 | z | e
6 | z | f
When I do this query:
select * from the_table group by col2
then in mysql I get:
1 x a
3 y c
5 z e
and in postgreSQL, I am getting error:
ERROR: column "the_table.col3" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select * from the_table group by col2;
My Questions:
What does this error mean? What is aggregate function ?
When it works in MySQL , why can't it work in postgreSQL ?
Alternatively on the MySQL answer: It wouldn't work in 5.7 version onwards.
You can use
ANY_VALUE()
function as stated in MySQL documentation.Sources: https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_any-value
Example:
You need to use
AGGREGATE FUNCTION
:SqlFiddleDemo
MySQL Handling of GROUP BY
:and:
So with MySQL version without explicit aggregate function you may end up with undetermininistic values. I strongly suggest to use specific aggregate function.
EDIT:
From MySQL Handling of GROUP BY:
Example:
Please, use it:
My result: