mysql - 如果点与它相交,请指定位置名称 MySQL
问题描述
我正在使用 MySQL 8.0
我有两张桌子:location_table、rent_table
location_tables 看起来像:
location_name polygon
A BLOB
B BLOB
...
多边形是 POLYGON 数据类型。
租表看起来像:
user_id rent_time rent_location
1 x BLOB
2 x BLOB
...
其中rent_location 是 POINT 数据类型
对于rent_table 中的每一行,我想创建一个列来指示它属于哪个location_name。如果 user_id rent_location 与 location_name = A 新列相交,则 A
它看起来像这样:
user_id rent_time rent_location location_name
1 x BLOB A
2 x BLOB B
...
提前致谢!
我试过的:
我可以通过使用来一一做
select *
, st_intersects(A, Point(ST_X(work_location), ST_Y(work_location))) as location_A
, st_intersects(B, Point(ST_X(work_location), ST_Y(work_location))) as location_B
, st_intersects(C, Point(ST_X(work_location), ST_Y(work_location))) as location_C
from rent_table;
这在我事先设置 A、B、C 变量但我想直接从 location_table 获取位置多边形时有效。
我可以使用如下子查询:
select *
, st_intersects((select polygon from location_table where location_name = 'A'), Point(ST_X(work_location), ST_Y(work_location))) as location_A
from rent_table;
但是我在rent_table 中有数百万行,因此我不希望select 语句中的子查询为每一百万行运行。
解决方案
你可以做:
select
r.*,
l.location_name
from rent r
left join location l on ST_CONTAINS(l.polygon, r.rent_location)
推荐阅读
- android - 如何在 android 中比较 mycurrent 日期和 Quarterwise
- php - 如何使用 ajax 和 jquery 调用具有所需数据的端点?
- javascript - Javascript - 序列化数组以发送到控制器
- asp.net-core - AzureADB2C登录后如何重定向到返回URL
- javascript - 您如何以设定的宽度格式化 JavaScript / html 字符串?
- java - 我们可以在原始数据类型包装类(例如整数)的实例上调用 finalize() 吗?如果不是为什么?
- kendo-ui - 有没有办法按类型对 Kendo UI 调度程序资源进行分组?
- javascript - 如何在调用 ReactDOM.render 之前获取 JSX 元素的 outerHTML?
- ruby-on-rails - Rails资产编译在docker compose中不起作用
- sql-server - CDS 中预过滤的表/实体、系统视图或存储过程(基于 Dynamics CRM DB)