2011-12-29 21 views
3

MySQLのステートメントを変更して、プロパティのIDが既にtable_bにない場合にのみ、table_bに行を挿入するようにしますか?MySQL:既に存在しない場合は行を挿入しますか?

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; 
+1

が重複する可能性を(HTTP ://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql) –

答えて

9

あなたはこの質問のように、MySQLのINSERT IGNORE構文を使用することができます。How to 'insert if not exists' in MySQL?

たとえば:[?MySQLの中に '存在しない場合は挿入' する方法]の

INSERT IGNORE INTO tbl1 (id, col) VALUES (1, 2); 
関連する問題