在mySQL和postgreSQL中按分组划分,为什么postgreSQL中的错误? [英] Group by clause in mySQL and postgreSQL, why the error in postgreSQL?

查看:185
本文介绍了在mySQL和postgreSQL中按分组划分,为什么postgreSQL中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个表:named = the_table
的结构为:



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))

然后我插入表格:

  INSERT INTO the_table(col2,col1)VALUES 
('x','a'),
('x','b'),
('y','c'),
('y','d '),
('z','e'),
('z','f');

现在表格如下所示:

  col3 | col2 | col1 
------ + ------ + ------
1 | x |
2 | x | b
3 | y | c
4 | y | d
5 | z | e
6 | z | f

当我执行此查询时:

 select * from the_table group by col2 

然后在mysql I中get:

  1 xa 
3 yc
5 ze

在postgreSQL中,我收到错误:

 错误:列the_table.col3必须出现在GROUP BY子句中或用于聚合函数
LINE 1:select * from thetable group by col2;

我的问题

这个错误是什么意思?
什么是聚合函数?



当它在MySQL中工作时,为什么它不能在postgreSQL中工作?

解决方案

您需要使用 $ b


集合函数通过一组输入
值计算单个结果。




  SELECT col2,MIN(col3)AS col3,MIN(col1)AS col1 
FROM the_table
GROUP BY col2;

SqlFiddleDemo



MySQL处理GROUP BY


标准SQL,包含GROUP BY子句的查询不能将
引用到选择列表中未在
GROUP BY子句中指定的非聚合列


和:


MySQL扩展了GROUP BY的用法,可以引用未在GROUP BY子句中命名的非聚集列。这意味着前面的查询在MySQL中是合法的。您可以使用此功能通过避免不必要的列排序和分组来获得更好的性能。但是,这非常有用,因为每个未在GROUP BY中命名的非聚合列中的所有值对于每个组都是相同的。 服务器可以自由选择每组中的任何值,因此除非它们相同,否则所选值是不确定的。

因此,对于没有显式集合函数的MySQL版本,您可能会得到不确定的值。我强烈建议使用特定的聚合函数。






编辑:



MySQL处理GROUP BY


SQL92及更早版本不允许查询选择列表,HAVING条件或ORDER BY列表



如果SQL99和更高版本在功能上依赖于GROUP BY,则它们允许每个可选功能T301 使用非聚合字段列:如果name和custid之间存在这种关系,则该查询是合法的。例如,这就是客户的主要关键。


示例:

  SELECT o.custid,c.name,MAX(o.payment)
FROM订单AS o
JOIN客户AS c
ON o.custid = c.custid
GROUP BY o。客户ID;


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 ?

解决方案

You need to use AGGREGATE FUNCTION:

Aggregate functions compute a single result from a set of input values.

SELECT col2, MIN(col3) AS col3, MIN(col1) AS col1
FROM the_table 
GROUP BY col2;

SqlFiddleDemo

MySQL Handling of GROUP BY:

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause

and:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate

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:

SQL92 and earlier does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause.

SQL99 and later permits such nonaggregates per optional feature T301 if they are functionally dependent on GROUP BY columns: If such a relationship exists between name and custid, the query is legal. This would be the case, for example, were custid a primary key of customers.

Example:

SELECT o.custid, c.name, MAX(o.payment)
FROM orders AS o
JOIN customers AS c
  ON o.custid = c.custid
GROUP BY o.custid;

这篇关于在mySQL和postgreSQL中按分组划分,为什么postgreSQL中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆