Search code examples
postgresqlwhere-clausesqldatatypes

How to select a specific row based on two values in PostgreSQL


I have a really strange problem. I know that the first row of my table has two columns with the following values: lat = 35.6635 lng = 139.395

The datatype is REAL (I have checked it).

I am trying to select these values with the following query:

select lat, lng from mytable where lat = 35.6635 and lng = 139.395

The output is nothing, i.e. I cannot find the values in my table.

Any idea what's the problem here?


Solution

  • The REAL type in Postgres is inexact, meaning that the exact values you used in your query may not actually be stored as is. You should use NUMERIC or DECIMAL if you require exact storage. See the Postgres documentation for more information about exact and inexact numeric types.

    As a workaround to find your record, you may try:

    select lat, lng
    from mytable
    where round(lat, 1) = 35.7 and round(lng, 1) = 139.4;
    

    Should the above query return more than one record, hopefully it would be clear to you which is the record you want to find.