Mysql 动态范围值
Mysql Range Dynamically with values
我有一个 table 存储我的级别和升级到另一个级别所需的点数,如下所示:
id | level | pointsrequired
-----------------------------
1 | 0 | 0
2 | 1 | 20
3 | 2 | 50
4 | 3 | 90
现在所需的点数可以是动态的,所以不确定我是否使用修复案例值,我想检查我当前的点数是否大于前一个点数且小于下一个点数我的等级应该升级,否则没问题。
这是我的查询,但不确定这个解决方案是否正确。
SELECT * FROM tbl WHERE pointsrequired BETWEEN 25 AND (SELECT MAX(pointsrequired) FROM tbl) LIMIT 1
像以前我的积分是0,现在我得到25那么,我应该升级到2级
任何其他对此不确定的解决方案。
我想这就是你想要的逻辑:
SELECT *
FROM tbl t1
WHERE
pointsrequired BETWEEN 25 AND
(SELECT t2.pointsrequired FROM tbl t2 WHERE t2.pointsrequired > 25
ORDER BY t2.pointsrequired LIMIT 1);
子查询查找下一个高于输入值 25 的直接级别。
SELECT `level` FROM tbl WHERE pointsrequired > 25 ORDER BY pointsrequired LIMIT 1
25 岁你想升到 2 级吗?
SELECT
`id`
, `level`
, `pointsrequired`
FROM
tbl
WHERE
pointsrequired <= 25
ORDER BY
pointsrequired DESC
LIMIT 1
我有一个 table 存储我的级别和升级到另一个级别所需的点数,如下所示:
id | level | pointsrequired
-----------------------------
1 | 0 | 0
2 | 1 | 20
3 | 2 | 50
4 | 3 | 90
现在所需的点数可以是动态的,所以不确定我是否使用修复案例值,我想检查我当前的点数是否大于前一个点数且小于下一个点数我的等级应该升级,否则没问题。
这是我的查询,但不确定这个解决方案是否正确。
SELECT * FROM tbl WHERE pointsrequired BETWEEN 25 AND (SELECT MAX(pointsrequired) FROM tbl) LIMIT 1
像以前我的积分是0,现在我得到25那么,我应该升级到2级 任何其他对此不确定的解决方案。
我想这就是你想要的逻辑:
SELECT *
FROM tbl t1
WHERE
pointsrequired BETWEEN 25 AND
(SELECT t2.pointsrequired FROM tbl t2 WHERE t2.pointsrequired > 25
ORDER BY t2.pointsrequired LIMIT 1);
子查询查找下一个高于输入值 25 的直接级别。
SELECT `level` FROM tbl WHERE pointsrequired > 25 ORDER BY pointsrequired LIMIT 1
25 岁你想升到 2 级吗?
SELECT
`id`
, `level`
, `pointsrequired`
FROM
tbl
WHERE
pointsrequired <= 25
ORDER BY
pointsrequired DESC
LIMIT 1