gpt4 book ai didi

mysql - 如何从内连接SQL中获取下一个值

转载 作者:行者123 更新时间:2023-11-29 11:43:08 26 4
gpt4 key购买 nike

这个问题是这个here的继续,因为他们不想在不打开新内容的情况下回答。

这是我的表格:

客户资料

enter image description here

friend 级别

enter image description here

所以我有一个查询来获取他来自哪个级别,所以当他获得points = 168时,他将获得Great Friend,所以这是我的SQL我已经有了。

SELECT s.*
FROM customer_profiles t
INNER JOIN friend_levels s ON(t.friend_points >= s.points_needed)
WHERE s.points_needed = (SELECT max(f.points_needed)
FROM friend_levels f
WHERE t.friend_points >= f.points_needed)
AND t.user_id = $user_id

结果是这样的

enter image description here

所以我的问题是如何获取它的下一个值以便像这样使用它?对于前。如果我处于好 friend 级别,那么我必须获得最好的 friend 级别。

enter image description here

最佳答案

这应该很简单:

SELECT (SELECT min(fl.points_needed) 
FROM friend_levels fl
WHERE fl.points_needed > cp.friend_points) - cp.friend_points AS points_needed_for_next_level
FROM customer_profiles cp
WHERE cp.user_id = $user_id;

关于mysql - 如何从内连接SQL中获取下一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35428684/

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