在 MySQL 中获取多点空间数据
Fetching Multipoint Spatial Data in MySQL
我没有找到任何答案如何 select 多点 数据类型中的每个点 MySQL。我有 包含许多点的多点,我想通过 query select 每个点,但我无法弄清楚。有任何想法吗?
Table结构:Image
示例数据:Image
在 MySQL 文档中我只找到了这个,但没有帮助:https://dev.mysql.com/doc/refman/8.0/en/fetching-spatial-data.html
如果您使用的是 MySQL >= 8.0.2,选项可能是:
DELIMITER //
CREATE PROCEDURE `sp_test`(`p_id` BIGINT UNSIGNED)
BEGIN
SET @`sql` := CONCAT('
WITH RECURSIVE `sequence` AS (
SELECT 1 `seq`
UNION ALL
SELECT `seq` + 1 `seq`
FROM `sequence`
WHERE `sequence`.`seq` < ?
)
SELECT
`sequence`.`seq` `id`,
ST_AsText(ST_GeometryN(`points`, `sequence`.`seq`)) `point`
FROM `sequence`
INNER JOIN `tb_test` ON
`tb_test`.`id` = ', `p_id`);
SET @`limit` := (
SELECT
ST_NumGeometries(`points`)
FROM
`tb_test`
WHERE
`id` = `p_id`
);
PREPARE `stmt` FROM @`sql`;
EXECUTE `stmt` USING @`limit`;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER ;
参见db-fiddle。
更新
DELIMITER //
CREATE PROCEDURE `sp_test`(`p_id` BIGINT UNSIGNED)
BEGIN
SET @`sql` := CONCAT('
WITH RECURSIVE `sequence` AS (
SELECT 1 `seq`
UNION ALL
SELECT `seq` + 1 `seq`
FROM `sequence`
WHERE `sequence`.`seq` < ?
)
SELECT
`sequence`.`seq` `id`,
ST_AsText(ST_GeometryN(`points`, `sequence`.`seq`)) `point`,
ST_X(ST_GeometryN(`points`, `sequence`.`seq`)) `X`,
ST_Y(ST_GeometryN(`points`, `sequence`.`seq`)) `Y`
FROM `sequence`
INNER JOIN `tb_test` ON
`tb_test`.`id` = ', `p_id`);
SET @`limit` := (
SELECT
ST_NumGeometries(`points`)
FROM
`tb_test`
WHERE
`id` = `p_id`
);
PREPARE `stmt` FROM @`sql`;
EXECUTE `stmt` USING @`limit`;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER ;
我没有找到任何答案如何 select 多点 数据类型中的每个点 MySQL。我有 包含许多点的多点,我想通过 query select 每个点,但我无法弄清楚。有任何想法吗?
Table结构:Image
示例数据:Image
在 MySQL 文档中我只找到了这个,但没有帮助:https://dev.mysql.com/doc/refman/8.0/en/fetching-spatial-data.html
如果您使用的是 MySQL >= 8.0.2,选项可能是:
DELIMITER //
CREATE PROCEDURE `sp_test`(`p_id` BIGINT UNSIGNED)
BEGIN
SET @`sql` := CONCAT('
WITH RECURSIVE `sequence` AS (
SELECT 1 `seq`
UNION ALL
SELECT `seq` + 1 `seq`
FROM `sequence`
WHERE `sequence`.`seq` < ?
)
SELECT
`sequence`.`seq` `id`,
ST_AsText(ST_GeometryN(`points`, `sequence`.`seq`)) `point`
FROM `sequence`
INNER JOIN `tb_test` ON
`tb_test`.`id` = ', `p_id`);
SET @`limit` := (
SELECT
ST_NumGeometries(`points`)
FROM
`tb_test`
WHERE
`id` = `p_id`
);
PREPARE `stmt` FROM @`sql`;
EXECUTE `stmt` USING @`limit`;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER ;
参见db-fiddle。
更新
DELIMITER //
CREATE PROCEDURE `sp_test`(`p_id` BIGINT UNSIGNED)
BEGIN
SET @`sql` := CONCAT('
WITH RECURSIVE `sequence` AS (
SELECT 1 `seq`
UNION ALL
SELECT `seq` + 1 `seq`
FROM `sequence`
WHERE `sequence`.`seq` < ?
)
SELECT
`sequence`.`seq` `id`,
ST_AsText(ST_GeometryN(`points`, `sequence`.`seq`)) `point`,
ST_X(ST_GeometryN(`points`, `sequence`.`seq`)) `X`,
ST_Y(ST_GeometryN(`points`, `sequence`.`seq`)) `Y`
FROM `sequence`
INNER JOIN `tb_test` ON
`tb_test`.`id` = ', `p_id`);
SET @`limit` := (
SELECT
ST_NumGeometries(`points`)
FROM
`tb_test`
WHERE
`id` = `p_id`
);
PREPARE `stmt` FROM @`sql`;
EXECUTE `stmt` USING @`limit`;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER ;