0

I have an SQL table that stores running times and a score associated with each time on the table.

/////////////////////
/ Time    *   Score /
/ 1531    *    64   /
/ 1537    *    63   /
/ 1543    *    61   /
/ 1549    *    60   /
/////////////////////

This is an example of 4 rows in the table. My question is how do I select the nearest lowest time.

EXAMPLE: If someone records a time of 1548 I want to return the score for 1543 (not 1549) which is 61.

Is there an SQL query I can use to do this thank you.

4

1 回答 1

9

使用 SQL 的WHERE子句过滤记录,使用它的ORDER BY子句对它们进行排序,并且LIMIT(在 MySQL 中)只获得第一个结果:

SELECT   Score
FROM     my_table
WHERE    Time <= 1548
ORDER BY Time DESC
LIMIT    1

sqlfiddle上查看。

于 2013-05-14T21:06:57.660 回答