#### 12.17.9.2 Spatial Relation Functions That Use Minimum Bounding Rectangles

[]()[]()

 MySQL provides several MySQL-specific functions that test the relationship between minimum bounding rectangles (MBRs) of two geometries *`g1`* and *`g2`*. The return values 1 and 0 indicate true and false, respectively.

 The bounding box of a point is interpreted as a point that is both boundary and interior.

 The bounding box of a straight horizontal or vertical line is interpreted as a line where the interior of the line is also boundary. The endpoints are boundary points.

 If any of the parameters are geometry collections, the interior, boundary, and exterior of those parameters are those of the union of all elements in the collection.

 Functions in this section detect arguments in either Cartesian or geographic spatial reference systems (SRSs), and return results appropriate to the SRS.

 Unless otherwise specified, functions in this section handle their geometry arguments as follows:

* If any argument is `NULL` or an empty geometry, the return value is `NULL`.

* If any geometry argument is not a syntactically well-formed geometry, an [`ER_GIS_INVALID_DATA`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.

* If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), an [`ER_SRS_NOT_FOUND`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_srs_not_found) error occurs.

* For functions that take multiple geometry arguments, if those arguments are not in the same SRS, an [`ER_GIS_DIFFERENT_SRIDS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_gis_different_srids) error occurs.

* If any argument is geometrically invalid, either the result is true or false (it is undefined which), or an error occurs.

* For geographic SRS geometry arguments, if any argument has a longitude or latitude that is out of range, an error occurs:

  * If a longitude value is not in the range (−180, 180], an [`ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_geometry_param_longitude_out_of_range) error occurs ([`ER_LONGITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_longitude_out_of_range) prior to MySQL 8.0.12).

  * If a latitude value is not in the range [−90, 90], an [`ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_geometry_param_latitude_out_of_range) error occurs ([`ER_LATITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_latitude_out_of_range) prior to MySQL 8.0.12).

   Ranges shown are in degrees. If an SRS uses another unit, the range uses the corresponding values in its unit. The exact range limits deviate slightly due to floating-point arithmetic.

* Otherwise, the return value is non-`NULL`.

 These MBR functions are available for testing geometry relationships:

* []()

  [`MBRContains(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrcontains)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangle of *`g1`* contains the minimum bounding rectangle of *`g2`*. This tests the opposite relationship as [`MBRWithin()`](spatial-relation-functions-mbr.html#function_mbrwithin).

  [`MBRContains()`](spatial-relation-functions-mbr.html#function_mbrcontains) handles its arguments as described in the introduction to this section.

  ```
  mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
  mysql> SET @g2 = ST_GeomFromText('Point(1 1)');
  mysql> SELECT MBRContains(@g1,@g2), MBRWithin(@g2,@g1);
  +----------------------+--------------------+
  | MBRContains(@g1,@g2) | MBRWithin(@g2,@g1) |
  +----------------------+--------------------+
  |                    1 |                  1 |
  +----------------------+--------------------+
  ```

* []()

  [`MBRCoveredBy(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrcoveredby)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangle of *`g1`* is covered by the minimum bounding rectangle of *`g2`*. This tests the opposite relationship as [`MBRCovers()`](spatial-relation-functions-mbr.html#function_mbrcovers).

  [`MBRCoveredBy()`](spatial-relation-functions-mbr.html#function_mbrcoveredby) handles its arguments as described in the introduction to this section.

  ```
  mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
  mysql> SET @g2 = ST_GeomFromText('Point(1 1)');
  mysql> SELECT MBRCovers(@g1,@g2), MBRCoveredby(@g1,@g2);
  +--------------------+-----------------------+
  | MBRCovers(@g1,@g2) | MBRCoveredby(@g1,@g2) |
  +--------------------+-----------------------+
  |                  1 |                     0 |
  +--------------------+-----------------------+
  mysql> SELECT MBRCovers(@g2,@g1), MBRCoveredby(@g2,@g1);
  +--------------------+-----------------------+
  | MBRCovers(@g2,@g1) | MBRCoveredby(@g2,@g1) |
  +--------------------+-----------------------+
  |                  0 |                     1 |
  +--------------------+-----------------------+
  ```

* []()

  [`MBRCovers(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrcovers)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangle of *`g1`* covers the minimum bounding rectangle of *`g2`*. This tests the opposite relationship as [`MBRCoveredBy()`](spatial-relation-functions-mbr.html#function_mbrcoveredby). See the description of [`MBRCoveredBy()`](spatial-relation-functions-mbr.html#function_mbrcoveredby) for examples.

  [`MBRCovers()`](spatial-relation-functions-mbr.html#function_mbrcovers) handles its arguments as described in the introduction to this section.

* []()

  [`MBRDisjoint(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrdisjoint)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries *`g1`* and *`g2`* are disjoint (do not intersect).

  [`MBRDisjoint()`](spatial-relation-functions-mbr.html#function_mbrdisjoint) handles its arguments as described in the introduction to this section.

* []()

  [`MBREquals(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrequals)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries *`g1`* and *`g2`* are the same.

  [`MBREquals()`](spatial-relation-functions-mbr.html#function_mbrequals) handles its arguments as described in the introduction to this section, except that it does not return `NULL` for empty geometry arguments.

* []()

  [`MBRIntersects(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrintersects)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries *`g1`* and *`g2`* intersect.

  [`MBRIntersects()`](spatial-relation-functions-mbr.html#function_mbrintersects) handles its arguments as described in the introduction to this section.

* []()

  [`MBROverlaps(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbroverlaps)

  []()

   Two geometries *spatially overlap* if they intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.

   This function returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries *`g1`* and *`g2`* overlap.

  [`MBROverlaps()`](spatial-relation-functions-mbr.html#function_mbroverlaps) handles its arguments as described in the introduction to this section.

* []()

  [`MBRTouches(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrtouches)

  []()

   Two geometries *spatially touch* if their interiors do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.

   This function returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries *`g1`* and *`g2`* touch.

  [`MBRTouches()`](spatial-relation-functions-mbr.html#function_mbrtouches) handles its arguments as described in the introduction to this section.

* []()

  [`MBRWithin(*`g1`*, *`g2`*)`](spatial-relation-functions-mbr.html#function_mbrwithin)

  []()

   Returns 1 or 0 to indicate whether the minimum bounding rectangle of *`g1`* is within the minimum bounding rectangle of *`g2`*. This tests the opposite relationship as [`MBRContains()`](spatial-relation-functions-mbr.html#function_mbrcontains).

  [`MBRWithin()`](spatial-relation-functions-mbr.html#function_mbrwithin) handles its arguments as described in the introduction to this section.

  ```
  mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
  mysql> SET @g2 = ST_GeomFromText('Polygon((0 0,0 5,5 5,5 0,0 0))');
  mysql> SELECT MBRWithin(@g1,@g2), MBRWithin(@g2,@g1);
  +--------------------+--------------------+
  | MBRWithin(@g1,@g2) | MBRWithin(@g2,@g1) |
  +--------------------+--------------------+
  |                  1 |                  0 |
  +--------------------+--------------------+
  ```

