Agustin Castro
Agustin Castro

Reputation: 459

bd relationship MySQL

I have the following relationship,

enter image description here

And wanted to know how I can get the team a game winner?

Upvotes: 0

Views: 67

Answers (1)

EthanB
EthanB

Reputation: 4289

SELECT
  m.id_match,
  IF((id_home_team = ? AND score_home_team > score_away_team) OR
    (id_away_team = ? AND score_home_team < score_away_team), 1, 0) AS won
FROM matches m
  JOIN results r ON(r.id_match = m.id_match)
WHERE 
  id_home_team = ? OR id_away_team = ?

Replace the ? with your team's ID.

In SQL Fiddle.

Upvotes: 1

Related Questions