MySQL/POINT:修订间差异
来自康健生活
< MySQL
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
|||
| (未显示同一用户的14个中间版本) | |||
| 第1行: | 第1行: | ||
== 测试数据 == | == 测试数据 == | ||
<div class="pageBoxGreen">mysql{{Version|8.0+}} 之后的版本,空间数据类型的操作函数在命名上统一在前面加上<code>ST</code>,如 <code>GeomFromText</code> 改为 <code>ST_GeomFromText</code>。</div> | |||
创建测试表和字段。 | 创建测试表和字段。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="mysql"> | ||
CREATE TABLE `test- | CREATE TABLE `test-location` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号', | `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号', | ||
` | `location` point NOT NULL COMMENT '经纬度', | ||
`text` varchar(50) DEFAULT NULL COMMENT '描述', | `text` varchar(50) DEFAULT NULL COMMENT '描述', | ||
PRIMARY KEY (`id`) | PRIMARY KEY (`id`) | ||
) ENGINE=MyISAM AUTO_INCREMENT= | ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;</syntaxhighlight>|{{consoleout|<pre> | ||
Query OK, 0 rows affected (0.08 sec) | Query OK, 0 rows affected (0.08 sec) | ||
</pre>}}}} | </pre>}}}} | ||
{{ | {{Double-columns|<syntaxhighlight lang="mysql"> | ||
INSERT INTO `test- | INSERT INTO `test-location` (location, text) VALUES | ||
(ST_GeomFromText('POINT(100 200)'), '测试点1'), | (ST_GeomFromText('POINT(100 200)'), '测试点1'), | ||
(ST_GeomFromText('POINT(300 500)'), '测试点2');</syntaxhighlight>|{{consoleout|<pre> | (ST_GeomFromText('POINT(300 500)'), '测试点2');</syntaxhighlight>|{{consoleout|<pre> | ||
Query OK, 2 rows affected (0.02 sec) | Query OK, 2 rows affected (0.02 sec) | ||
Records: 2 Duplicates: 0 Warnings: 0 | Records: 2 Duplicates: 0 Warnings: 0 | ||
</pre>}}}} | |||
添加数据还可以使用其他语句。 | |||
<syntaxhighlight lang="mysql"> | |||
SET @tempdata = 'POINT(100 100)'; | |||
INSERT INTO `test-location`(location, text) VALUES ( | |||
ST_GeomFromText(@tempdata), | |||
'测试点3' | |||
); | |||
/** or **/ | |||
SET @tempdata = ST_GeomFromText('POINT(100 100)'); | |||
INSERT INTO `test-location`(location, text) VALUES ( | |||
@tempdata, | |||
'测试点4' | |||
); | |||
</syntaxhighlight> | |||
使用 mysql{{Version|5.6.48}} 测试坐标点。 | |||
{{Double-columns|<syntaxhighlight lang="mysql"> | |||
select geomfromtext('Point(10.1234 20.5678)'); | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
AAAAAAEBAAAAHqfoSC4/JEA2qz5XW5E0QA== | |||
</pre>}}}} | |||
使用 mysql{{Version|8.0.28}} 测试坐标点。 | |||
{{Double-columns|<syntaxhighlight lang="mysql"> | |||
select st_geomfromtext('Point(10.1234 20.5678)'); | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
+-------------------------------------------+ | |||
| st_geomfromtext('Point(10.2045 34.2379)') | | |||
+-------------------------------------------+ | |||
| POINT(10.2045 34.2379) | | |||
+-------------------------------------------+ | |||
1 row in set (0.00 sec) | |||
</pre>}}}} | |||
== 函数 == | |||
=== Point(X,Y) === | |||
Point(X,Y) 是具有精确度的数字的构造函数,不需要先转换为文本,所以速度更快。 | |||
=== ST_GeomFromText === | |||
ST_GeomFromText(wkt, srid)可以返回 MySQL 支持的任何空间类型,可以用 WKT 表示。 | |||
=== ST_PointFromText === | |||
ST_PointFromText(wkt, srid)POINT 来自 Well-known text的强类型构造函数。 | |||
== 查询数据 == | |||
{{Double-columns|<syntaxhighlight lang="mysql"> | |||
SELECT | |||
id, | |||
ST_x(location) x, | |||
ST_y(location) y, | |||
location, | |||
text | |||
FROM `test-location`; | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
+----+-----+-----+----------------+------------+ | |||
| id | x | y | location | text | | |||
+----+-----+-----+----------------+------------+ | |||
| 1 | 100 | 200 | POINT(100 200) | 测试点1 | | |||
| 2 | 300 | 500 | POINT(300 500) | 测试点2 | | |||
+----+-----+-----+----------------+------------+ | |||
2 rows in set (0.01 sec) | |||
</pre>}}}} | |||
ST_AsText | |||
{{Double-columns|<syntaxhighlight lang="mysql"> | |||
SELECT id, ST_AsText(location), text FROM `test-location`;</syntaxhighlight>|{{consoleout|<pre> | |||
+----+---------------------+------------+ | |||
| id | ST_AsText(location) | text | | |||
+----+---------------------+------------+ | |||
| 1 | POINT(100 200) | 测试点1 | | |||
| 2 | POINT(300 500) | 测试点2 | | |||
+----+---------------------+------------+ | |||
2 rows in set (0.00 sec) | |||
</pre>}}}} | |||
== 更新数据 == | |||
{{Double-columns|<syntaxhighlight lang="mysql"> | |||
update `test-location` set location=st_GeomFromText('POINT(123 100)') where id =1;</syntaxhighlight>|{{consoleout|<pre> | |||
Query OK, 1 row affected (0.01 sec) | |||
Rows matched: 1 Changed: 1 Warnings: 0 | |||
</pre>}}}} | </pre>}}}} | ||
2026年11月26日 (四) 05:28的最新版本
测试数据
mysql 之后的版本,空间数据类型的操作函数在命名上统一在前面加上
ST,如 GeomFromText 改为 ST_GeomFromText。创建测试表和字段。
CREATE TABLE `test-location` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`location` point NOT NULL COMMENT '经纬度',
`text` varchar(50) DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.08 sec)
INSERT INTO `test-location` (location, text) VALUES
(ST_GeomFromText('POINT(100 200)'), '测试点1'),
(ST_GeomFromText('POINT(300 500)'), '测试点2');
Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0
添加数据还可以使用其他语句。
SET @tempdata = 'POINT(100 100)';
INSERT INTO `test-location`(location, text) VALUES (
ST_GeomFromText(@tempdata),
'测试点3'
);
/** or **/
SET @tempdata = ST_GeomFromText('POINT(100 100)');
INSERT INTO `test-location`(location, text) VALUES (
@tempdata,
'测试点4'
);
使用 mysql 测试坐标点。
select geomfromtext('Point(10.1234 20.5678)');
AAAAAAEBAAAAHqfoSC4/JEA2qz5XW5E0QA==
使用 mysql 测试坐标点。
select st_geomfromtext('Point(10.1234 20.5678)');
+-------------------------------------------+
| st_geomfromtext('Point(10.2045 34.2379)') |
+-------------------------------------------+
| POINT(10.2045 34.2379) |
+-------------------------------------------+
1 row in set (0.00 sec)
函数
Point(X,Y)
Point(X,Y) 是具有精确度的数字的构造函数,不需要先转换为文本,所以速度更快。
ST_GeomFromText
ST_GeomFromText(wkt, srid)可以返回 MySQL 支持的任何空间类型,可以用 WKT 表示。
ST_PointFromText
ST_PointFromText(wkt, srid)POINT 来自 Well-known text的强类型构造函数。
查询数据
SELECT
id,
ST_x(location) x,
ST_y(location) y,
location,
text
FROM `test-location`;
+----+-----+-----+----------------+------------+ | id | x | y | location | text | +----+-----+-----+----------------+------------+ | 1 | 100 | 200 | POINT(100 200) | 测试点1 | | 2 | 300 | 500 | POINT(300 500) | 测试点2 | +----+-----+-----+----------------+------------+ 2 rows in set (0.01 sec)
ST_AsText
SELECT id, ST_AsText(location), text FROM `test-location`;
+----+---------------------+------------+ | id | ST_AsText(location) | text | +----+---------------------+------------+ | 1 | POINT(100 200) | 测试点1 | | 2 | POINT(300 500) | 测试点2 | +----+---------------------+------------+ 2 rows in set (0.00 sec)
更新数据
update `test-location` set location=st_GeomFromText('POINT(123 100)') where id =1;
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
No categories