File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
sql-queries-12/ideal-data-type-latitude-longitude-mysql Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ -- Create example table
2
+ CREATE TABLE Coordinates (
3
+ id INT AUTO_INCREMENT PRIMARY KEY ,
4
+ location VARCHAR (255 ),
5
+ latitude DECIMAL (10 , 8 ) NOT NULL ,
6
+ longitude DECIMAL (11 , 8 ) NOT NULL
7
+ );
8
+ -- Add example data
9
+ INSERT INTO Coordinates (location, latitude, longitude)
10
+ VALUES (' Eiffel Tower' , 48 .8584 , 2 .2945 );
11
+ -- Query example data
12
+ SELECT * FROM Coordinates;
Original file line number Diff line number Diff line change
1
+ -- Create example table
2
+ CREATE TABLE Locations (
3
+ id INT AUTO_INCREMENT PRIMARY KEY ,
4
+ name VARCHAR (255 ),
5
+ coordinates POINT NOT NULL SRID 4326 ,
6
+ SPATIAL INDEX(coordinates)
7
+ );
8
+ -- Add example data
9
+ INSERT INTO Locations (name, coordinates)
10
+ VALUES (' Eiffel Tower' , ST_PointFromText(' POINT(2.2945 48.8584)' , 4326 ));
11
+ INSERT INTO Locations (name, coordinates)
12
+ VALUES (' Arc de Triomphe' , ST_PointFromText(' POINT(2.2950 48.8738)' , 4326 ));
13
+ -- Query example data
14
+ SELECT
15
+ ST_Distance_Sphere(t1 .coordinates , t2 .coordinates ) AS Distance_in_Meters
16
+ FROM
17
+ Locations AS t1,
18
+ Locations AS t2
19
+ WHERE
20
+ t1 .name = ' Eiffel Tower' AND t2 .name = ' Arc de Triomphe' ;
You can’t perform that action at this time.
0 commit comments