Saya tidak yakin bagaimana hasil Anda terkait dengan matches
tabel,
Anda dapat menambahkan kembali WHERE
/ INNER JOIN
klausa jika Anda membutuhkannya.
Ini tautan ke biola
dan iterasi terakhir sesuai dengan komentar Anda:
Dan ini pertanyaannya:
SET @user:=0;
select d.user_id,
sum(case when d.result = 1 then 1 else 0 end) as winnings,
sum(case when d.result = 2 then 1 else 0 end) as loses,
sum(case when d.result = 1 then d.odd_value else 0 end) as points,
f.bonus
FROM odds d
INNER JOIN
(
SELECT
user_id,SUM(bonus) AS bonus
FROM
(
SELECT
user_id,
CASE WHEN result=1 and @counter<5 AND @user=user_id THEN @counter:[email protected]+1
WHEN result=1 and @counter=5 AND @user=user_id THEN @counter:=1
WHEN result=1 and @user<>user_id THEN @counter:=1
ELSE
@counter:=0
END AS F1,
@user:=user_id,
CASE WHEN @counter=5 THEN 1 ELSE 0 END AS bonus
FROM odds o
ORDER BY user_id , match_id
) Temp
group by user_id
)as f on f.user_id = d.user_id
group by d.user_id