第一部分。如何在两个不同的“AS”列上搜索值
第2部分。如何将前5个最接近的值列为98?
查询示例:
select
TITLE.name,
(TITLE.value-TITLE.msp) AS Lower,
(TITLE.value+TITLE.msp) AS Upper
FROM TITLE
WHERE 98 BETWEEN Lower AND Upper;
答案 0 :(得分:3)
对于第1部分),您需要使用嵌套选择,因为您不能在同一子选择中使用重命名的投影(SELECT
部分)作为谓词(WHERE
部分)。
对于第2部分),使用ORDER BY .. LIMIT
获得前5名。按最小绝对差值排序98
SELECT t.* FROM (
SELECT
TITLE.name,
(TITLE.value-TITLE.msp) AS Lower,
(TITLE.value+TITLE.msp) AS Upper,
(TITLE.value) AS Value
FROM TITLE
) t
WHERE 98 BETWEEN t.Lower AND t.Upper
ORDER BY ABS(98 - t.Value) ASC
LIMIT 5