MySQL supports aggregate functions that perform a calculation on a set of values. MySQL支持对一组值执行计算的聚合函数。For general information about these functions, see Section 12.20.1, “Aggregate Function Descriptions”. 有关这些函数的一般信息,请参阅第12.20.1节,“聚合函数描述”。This section describes the 本节介绍ST_Collect() spatial aggregate function.ST_Collect()空间聚合函数。
ST_Collect() can be used as a window function, as signified in its syntax description by [, representing an optional over_clause]OVER clause. ST_Collect()可以用作窗口函数,如语法描述中的[所示,表示可选的over_clause]OVER子句。over_clause is described in Section 12.21.2, “Window Function Concepts and Syntax”, which also includes other information about window function usage.over_clause在第12.21.2节,“窗口函数概念和语法”中进行了描述,其中还包括有关窗口函数用法的其他信息。
ST_Collect([DISTINCT] g) [over_clause]
Aggregates geometry values and returns a single geometry collection value. 聚合几何体值并返回单个几何体集合值。With the 使用DISTINCT option, returns the aggregation of the distinct geometry arguments.DISTINCT选项,返回各不相同的几何参数的聚合。
As with other aggregate functions, 与其他聚合函数一样,GROUP BY may be used to group arguments into subsets. GROUP BY可用于将参数分组到子集中。ST_Collect() returns an aggregate value for each subset.ST_Collect()返回每个子集的聚合值。
This function executes as a window function if 如果存在over_clause is present. over_clause,则此函数作为窗口函数执行。over_clause is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause如第12.21.2节,“窗口函数概念和语法”所述。In constrast to most aggregate functions that support windowing, 与大多数支持窗口的聚合函数相比,ST_Collect() permits use of over_clause together with DISTINCT.ST_Collect()允许将over_clause与DISTINCT一起使用。
ST_Collect() handles its arguments as follows:ST_Collect()按如下方式处理其参数:
忽略NULL arguments are ignored.NULL参数。
If all arguments are 如果所有参数都为NULL or the aggregate result is empty, the return value is NULL.NULL或聚合结果为空,则返回值为NULL。
If any geometry argument is not a syntactically well-formed geometry, an 如果任一个几何参数不是语法格式良好的几何体,则会发生ER_GIS_INVALID_DATA error occurs.ER_GIS_INVALID_DATA错误。
If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), an 如果任何几何参数是未定义空间参考系(SRS)中语法结构良好的几何体,则会发生ER_SRS_NOT_FOUND error occurs.ER_SRS_NOT_FOUND错误。
If there are multiple geometry arguments and those arguments are in the same SRS, the return value is in that SRS. 如果有多个几何参数并且这些参数在同一个SRS中,则返回值在该SRS中。If those arguments are not in the same SRS, an 如果这些参数不在同一SRS中,则会发生ER_GIS_DIFFERENT_SRIDS_AGGREGATION error occurs.ER_GIS_DIFFERENT_SRIDS_AGGREGATION错误。
The result is the narrowest 结果是最窄的Multi or XxxGeometryCollection value possible, with the result type determined from the non-NULL geometry arguments as follows:MultiXxx或GeometryCollection值,结果类型由非空几何参数确定,如下所示:
If all arguments are 如果所有参数都是Point values, the result is a MultiPoint value.Point值,则结果是MultiPoint值。
If all arguments are 如果所有参数都是LineString values, the result is a MultiLineString value.LineString值,则结果是一个MultiLineString值。
If all arguments are 如果所有参数都是Polygon values, the result is a MultiPolygon value.Polygon值,则结果是MultiPolygon值。
Otherwise, the arguments are a mix of geometry types and the result is a 否则,参数是几何体类型的混合,结果是GeometryCollection value.GeometryCollection值。
This example data set shows hypothetical products by year and location of manufacture:此示例数据集按年份和制造地点显示假设产品:
CREATE TABLE product (
year INTEGER,
product VARCHAR(256),
location Geometry
);
INSERT INTO product
(year, product, location) VALUES
(2000, "Calculator", ST_GeomFromText('point(60 -24)',4326)),
(2000, "Computer" , ST_GeomFromText('point(28 -77)',4326)),
(2000, "Abacus" , ST_GeomFromText('point(28 -77)',4326)),
(2000, "TV" , ST_GeomFromText('point(38 60)',4326)),
(2001, "Calculator", ST_GeomFromText('point(60 -24)',4326)),
(2001, "Computer" , ST_GeomFromText('point(28 -77)',4326));
Some sample queries using 对数据集使用ST_Collect() on the data set:ST_Collect()的一些示例查询:
mysql>SELECT ST_AsText(ST_Collect(location)) AS resultFROM product;+------------------------------------------------------------------+ | result | +------------------------------------------------------------------+ | MULTIPOINT((60 -24),(28 -77),(28 -77),(38 60),(60 -24),(28 -77)) | +------------------------------------------------------------------+ mysql>SELECT ST_AsText(ST_Collect(DISTINCT location)) AS resultFROM product;+---------------------------------------+ | result | +---------------------------------------+ | MULTIPOINT((60 -24),(28 -77),(38 60)) | +---------------------------------------+ mysql>SELECT year, ST_AsText(ST_Collect(location)) AS resultFROM product GROUP BY year;+------+------------------------------------------------+ | year | result | +------+------------------------------------------------+ | 2000 | MULTIPOINT((60 -24),(28 -77),(28 -77),(38 60)) | | 2001 | MULTIPOINT((60 -24),(28 -77)) | +------+------------------------------------------------+ mysql>SELECT year, ST_AsText(ST_Collect(DISTINCT location)) AS resultFROM product GROUP BY year;+------+---------------------------------------+ | year | result | +------+---------------------------------------+ | 2000 | MULTIPOINT((60 -24),(28 -77),(38 60)) | | 2001 | MULTIPOINT((60 -24),(28 -77)) | +------+---------------------------------------+ # selects nothing mysql>SELECT ST_Collect(location) AS resultFROM product WHERE year = 1999;+--------+ | result | +--------+ | NULL | +--------+ mysql>SELECT ST_AsText(ST_Collect(location)OVER (ORDER BY year, product ROWS BETWEEN 1 PRECEDING AND CURRENT ROW))AS resultFROM product;+-------------------------------+ | result | +-------------------------------+ | MULTIPOINT((28 -77)) | | MULTIPOINT((28 -77),(60 -24)) | | MULTIPOINT((60 -24),(28 -77)) | | MULTIPOINT((28 -77),(38 60)) | | MULTIPOINT((38 60),(60 -24)) | | MULTIPOINT((60 -24),(28 -77)) | +-------------------------------+
This function was added in MySQL 8.0.24.MySQL 8.0.24中增加了这个函数。