$score = DB::table('u_games')
->where('home_team_id', $team_id)
->orWhere('away_team_id', $team_id)
->sum('home_score');
May 6, 2021
4
Level 6
How can use an IF/ELSE statement in my SQL query?
SELECT sum(home_score) as C FROM u_games WHERE (home_team_id ='$team_id' OR away_team_id = '$team_id')
I need to run one query if $team_id = home_team_id and a different one if $team_id = away_team_id
I'm trying to get the total score of a team. But I don't know beforehand whether it's home or away. It's random.
Thanks
Level 104
You should be able to achieve what you need using a CASE statement, e.g.
select sum(
case
when away_team_id = :team_id then away_score
when home_team_id = :team_id then home_score
end
) as C
from u_games
where (away_team_id = :team_id or home_team_id = :team_id)
Please or to participate in this conversation.