Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一张桌子results。
results
表有名称和金额列
那里有成千上万的记录。有些有数百个,有些有数千个,有些是 100k,有些是数千万。
我从 url 得到$amount作为变量。
我只想要 6 个更接近$amount的结果。
这次我使用mysql 的between子句。但问题是我怎么知道我使用了多少低于或高于 $amount。
取绝对差值,然后用于limit获取前几位:
limit
SELECT * FROM `numbers` ORDER BY ABS( amount - $amount ) ASC LIMIT 6
您可以简单地尝试以下查询,
SELECT name, amount, ABS( amount - $amount ) AS diff FROM results ORDER BY diff LIMIT 6