MySQL:如果行尚不存在,则插入行?

发布于 2024-12-23 07:12:36 字数 1495 浏览 1 评论 0原文

如何更改 MySQL 语句,以便仅在属性的 id 不在 table_b 中时才将行插入到 table_b 中?

INSERT INTO table_b( property_id, siteaddress, area_name, result) 
SELECT property_id, siteaddress,   "Area A" AS area_name,  IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((44.933690000000006, -111.07178
44.96479, -104.1504
41.062780000000004, -104.04053
41.01306, -111.07178
44.887010000000004, -111.04981000000001
))' )
)AS result
FROM table_a
WHERE IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((44.933690000000006, -111.07178
44.96479, -104.1504
41.062780000000004, -104.04053
41.01306, -111.07178
44.887010000000004, -111.04981000000001 ))' )
) = 1;

INSERT INTO table_b( property_id, siteaddress, area_name, result) 
SELECT property_id, siteaddress,   "Area B" AS area_name,  IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((37.909530000000004, -87.69288
37.89219000000001, -82.5293
36.40359, -83.58399
35.78217, -86.33057000000001
37.90872, -87.69356
37.909530000000004, -87.69288
))' )
)AS result
FROM table_a
WHERE IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((37.909530000000004, -87.69288
37.89219000000001, -82.5293
36.40359, -83.58399
35.78217, -86.33057000000001
37.90872, -87.69356
37.909530000000004, -87.69288 ))' )
) = 1;

How do I change my MySQL statement so that it only inserts a row into table_b if the property's id isn't already in table_b?

INSERT INTO table_b( property_id, siteaddress, area_name, result) 
SELECT property_id, siteaddress,   "Area A" AS area_name,  IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((44.933690000000006, -111.07178
44.96479, -104.1504
41.062780000000004, -104.04053
41.01306, -111.07178
44.887010000000004, -111.04981000000001
))' )
)AS result
FROM table_a
WHERE IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((44.933690000000006, -111.07178
44.96479, -104.1504
41.062780000000004, -104.04053
41.01306, -111.07178
44.887010000000004, -111.04981000000001 ))' )
) = 1;

INSERT INTO table_b( property_id, siteaddress, area_name, result) 
SELECT property_id, siteaddress,   "Area B" AS area_name,  IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((37.909530000000004, -87.69288
37.89219000000001, -82.5293
36.40359, -83.58399
35.78217, -86.33057000000001
37.90872, -87.69356
37.909530000000004, -87.69288
))' )
)AS result
FROM table_a
WHERE IS_POINT_IN_POLYGON(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON((37.909530000000004, -87.69288
37.89219000000001, -82.5293
36.40359, -83.58399
35.78217, -86.33057000000001
37.90872, -87.69356
37.909530000000004, -87.69288 ))' )
) = 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦等七年七年为一梦 2024-12-30 07:12:37

您可以使用 MySQL 的 INSERT IGNORE 语法,如本问题所示:如何在 MySQL 中“如果不存在则插入”?

例如:

INSERT IGNORE INTO tbl1 (id, col) VALUES (1, 2);

You can use MySQL's INSERT IGNORE syntax as in this question: How to 'insert if not exists' in MySQL?

For example:

INSERT IGNORE INTO tbl1 (id, col) VALUES (1, 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文