所以假设我有两张桌子。
表用户:
id |名字|日期
table weight_tracking;
id | user_id | previous_weight | current_weight |日期
每天都会根据用户当前体重和previous_weight更新weight_tracking表。
我正在尝试显示所有按previous_weight排序的用户 - current_weight(因此通过体重差异 - 谁失去了最多的体重将首先显示)
这可以通过1次电话完成吗?
答案 0 :(得分:0)
SELECT users.*, weight_tracking.*
FROM users left join weight_tracking on users.id = weight_tracking.user_id
ORDER BY previous_weight - current_weight desc
如果用户在weight_tracking表中可以有多行,则可以使用以下内容:
SELECT users.*
FROM users left join weight_tracking on users.id = weight_tracking.user_id
GROUP BY users.id
ORDER BY max(previous_weight) - min(current_weight) desc