Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select two nearly fields

Tags:

sql

select

How do I select two most nearly fields to specific timestamp?

SELECT * 
FROM 'wp_weather' 
WHERE ( timestamp most nearly to 1385435000) AND city = 'Махачкала'

The table:

id | timestamp
---------------
 0 | 1385410000
 1 | 1385420000
 2 | 1385430000
 3 | 1385440000
 4 | 1385450000

2 Answers

SELECT * 
FROM wp_weather
WHERE city = 'Махачкала'
order by abs(timestamp - 1385435000) 
limit 2
like image 99
juergen d Avatar answered Dec 08 '25 15:12

juergen d


You may try like this:

SELECT * FROM 'wp_weather' 
WHERE city = 'Махачкала'
order by abs(timestamp - 1385435000) 
limit 2

Also check the ABS function

like image 42
Rahul Tripathi Avatar answered Dec 08 '25 14:12

Rahul Tripathi