SQL选择两个近似字段

时间:2013-11-25 18:39:20

标签: sql select

如何选择两个最接近字段到特定时间戳?

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

表格:

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

您可以尝试这样:

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

同时检查ABS function