2016-03-09 86 views
1

我有這兩個表,第一個被稱爲item_coordinates,該類型是INT,DOUBLE,DOUBLE如何將一個表作爲值插入到另一個表中?

itemID  | latitude | longitude 
-------------+-----------+----------- 
1679323860 | 36.531398 | -82.98085 
1679340420 | 29.178171 | -74.075391 
1679386982 | 40.73235 | -94.6884 

現在我有一個名爲地理座標另一個表,我創造了這個表如下:

CREATE TABLE Geocoordinates (ItemID INT PRIMARY KEY, 
Geo_Coordinates POINT) ENGINE = MyISAM; 

現在我想將表1中的值插入到表2中,但不斷收到錯誤消息。這是我的嘗試:

INSERT INTO Geocoordinates (ItemID, Geo_Coordinates) VALUES (item_id, 
POINT(latitude, longitude)) SELECT item_id, latitude, longitude FROM 
item_coordinates); 

在此先感謝。

+0

SELECT INTO語句副本從一個表中的數據並將其插入到一個新表。我會研究那個.. – Zak

回答

0

我認爲你需要使用CONCAT功能組緯度/長成一個字段

INSERT INTO Geocoordinates (ItemID, Geo_Coordinates) VALUES (item_id, 
POINT(latitude, longitude)) SELECT item_id, CONCAT(latitude, longitude) FROM item_coordinates); 

我不認爲一點是需要在這種情況下。

0

使用INSERT INTO SELECT,不應指定VALUES關鍵字。以下是完整的演示。

SQL:

-- Data 
create table item_coordinates(itemID bigint, latitude decimal(10,6), longitude decimal(10,6)); 
CREATE TABLE Geocoordinates (ItemID INT PRIMARY KEY, 
Geo_Coordinates POINT) ENGINE = MyISAM; 
INSERT INTO item_coordinates values 
(1679323860 , 36.531398 , -82.98085), 
(1679340420 , 29.178171 , -74.075391), 
(1679386982 , 40.73235 , -94.6884); 
SELECT * FROM item_coordinates; 

-- SQL needed 
INSERT INTO Geocoordinates (itemID, Geo_Coordinates) SELECT itemID, POINT(latitude, longitude) FROM item_coordinates; 
SELECT itemID, ST_AsText(Geo_Coordinates) FROM Geocoordinates; 

輸出:

mysql> SELECT * FROM item_coordinates; 
+------------+-----------+------------+ 
| itemID  | latitude | longitude | 
+------------+-----------+------------+ 
| 1679323860 | 36.531398 | -82.980850 | 
| 1679340420 | 29.178171 | -74.075391 | 
| 1679386982 | 40.732350 | -94.688400 | 
+------------+-----------+------------+ 
3 rows in set (0.00 sec) 

mysql> INSERT INTO Geocoordinates (itemID, Geo_Coordinates) SELECT itemID, POINT(latitude, longitude) FROM item_coordinates; 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

mysql> SELECT itemID, ST_AsText(Geo_Coordinates) FROM Geocoordinates; 
+------------+-----------------------------+ 
| itemID  | ST_AsText(Geo_Coordinates) | 
+------------+-----------------------------+ 
| 1679323860 | POINT(36.531398 -82.98085) | 
| 1679340420 | POINT(29.178171 -74.075391) | 
| 1679386982 | POINT(40.73235 -94.6884) | 
+------------+-----------------------------+ 
3 rows in set (0.00 sec) 
相關問題