gpt4 book ai didi

php - 如何从数据库中查找范围值

转载 作者:行者123 更新时间:2023-11-29 06:16:48 24 4
gpt4 key购买 nike

如何根据数据库中存储的范围查找值。

我的friend_levels 表:

enter image description here

在我的个人资料表上:

enter image description here

当我有 friend_points = 1 时,我将获得 Normal Rate,然后如果我有 friend_points = 180,我将获得 Great Friend 所以在编程上基本上就是这样

if($profile_points >= 0 && $profile_points < 50) {
return 'Normal Rate';
} else if($profile_points >= 50 && $profile_points < 100) {
return 'Friend';
} else if($profile_points >= 100 && $profile_points < 150) {
return 'Good Friend';
}....

我的问题也是在 QUERY 上是否可行?或者我只是在 PHP 上实现?

EDIT1:有没有办法获得下一个目标值?对于前。如果我在 68 点好友率 上,如何获得 100 = Good Friend?不要介意减法,我只想得到下一行。

enter image description here

最佳答案

如果我没理解错的话,你可以像这样使用 CASE EXPRESSION:

SELECT id,user_id,
case when friend_points between 0 and 49 then 'Normal rate'
when friend_points between 50 and 99 then 'Friend'
when friend_points between 100 and 149 then 'Good friend'
.......
end as 'Friend_Status'
FROM profile

编辑:

或者,如果这个名字可以通过连接动态改变:

SELECT t.id,t.user_id,s.name
FROM profile t
INNER JOIN friend_levels s ON(t.friend_points >= s.points_needed)
WHERE s.points_needed = (select min(f.points_needed)
from friend_levels f
where t.friend_points >= f.points_needed)

关于php - 如何从数据库中查找范围值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35410072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com