2

How do I know if a pair of FLOAT(11,7) primary keys already exists?

I have these values lat: 50.0077953 and lng: -119.3953705

I do:

SELECT * FROM wp_geo_mashup_locations WHERE lat = '50.0077953' AND lng = '-119.395367'

Since this success, I do,

INSERT INTO wp_geo_mashup_locations (lat,lng) VALUES ('50.0077953', '-119.395367')

I get:

    Duplicate entry '50.0077934--119.3953705' for key 2
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Roger W.
  • 327
  • 1
  • 4
  • 15

1 Answers1

0

Well, if the INSERT fails, you know the pair already exists.

Alternatively, use some threshold when searching, eg:

lat BETWEEN 50.0077953-0.00005 AND 50.0077953+0.00005.

Note: you have play with this a bit till you figure out the right threshold (I don't know the resolution needed for long and lat coordinates).

Note: What is the ideal data type to use when storing latitude / longitudes in a MySQL database? - People suggest using spatial extensions, DECIMAL or FLOAT(10,6) / FLOAT(9,6) here.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176