我有這張桌子...
有沒有辦法獲得與 id 1 關聯的總和?
最后的結果應該是……4300。
問題圖片
uj5u.com熱心網友回復:
您可以使用遞回 CTE 來獲取所有鏈接的用戶:
with recursive u as
(select t.id, t.`Referral id`, t.Balance from yourtable t where id = 1
union
select t.id, t.`Referral id`, t.Balance from u inner join yourtable t
on u.id = t.`Referral id`)
(select sum(Balance) from u)
小提琴
uj5u.com熱心網友回復:
select Sum(Balance)
from YourTableName
where id=Referelid
and Referelid <=4
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/391745.html
上一篇:我無法運行Rails服務器
