popop
popop

Reputation: 177

How to select specific value on join condition?

I have a table that contains the details of a game, this table is called match, each match can have n goals, these goals are stored inside the table goal.

Suppose I have the following situation on match:

id = 2564824
home_team_half_time_score = 2
home_team_score = 4
away_team_half_time_score = 0
away_team_score = 0

so this record means that the match is ended 4 - 0 for the home team, what I want to know is: how can I get only the goals scored in the first time? in this case there are 2 goals scored in the first time, and have this id:

id    | match_id | result | minute 
16092   2564824    1 - 0     15
16093   2564824    2 - 0     43
16094   2564824    3 - 0     63
16095   2564824    4 - 0     78

I just need to get the minute field of each goal, how can I setup the query?

SCHEMA

CREATE TABLE IF NOT EXISTS `swp`.`match` (
  `id` INT NOT NULL,
  `round_id` INT NULL,
  `datetime` DATETIME NULL,
  `status` INT NULL,
  `gameweek` INT NULL,
  `home_team_id` INT NULL,
  `home_team_half_time_score` INT NULL,
  `home_team_score` INT NULL,
  `home_extra_time` INT NULL,
  `home_penalties` INT NULL,
  `away_team_id` INT NULL,
  `away_team_half_time_score` INT NULL,
  `away_team_score` INT NULL,
  `away_extra_time` INT NULL,
  `away_penalties` INT NULL,
  `venue_id` INT NULL,
  `venue_attendance` INT NULL,
  `aggregate_match_id` INT NULL,
  PRIMARY KEY (`id`),
  INDEX `home_team_id_idx` (`home_team_id` ASC),
  INDEX `away_team_id_idx` (`away_team_id` ASC),
  INDEX `venue_id_idx` (`venue_id` ASC),
  INDEX `match_status_id_idx` (`status` ASC),
  INDEX `FK_competition_rounds_match_round_id_idx` (`round_id` ASC),
  INDEX `FK_match_match_aggregate_match_id_idx` (`aggregate_match_id` ASC),
  CONSTRAINT `FK_team_match_home_team_id`
    FOREIGN KEY (`home_team_id`)
    REFERENCES `swp`.`team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_team_match_away_team_id`
    FOREIGN KEY (`away_team_id`)
    REFERENCES `swp`.`team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_venue_match_venue_id`
    FOREIGN KEY (`venue_id`)
    REFERENCES `swp`.`venue` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_match_status_match_status_id`
    FOREIGN KEY (`status`)
    REFERENCES `swp`.`match_status` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_competition_rounds_match_round_id`
    FOREIGN KEY (`round_id`)
    REFERENCES `swp`.`competition_rounds` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_match_match_aggregate_match_id`
    FOREIGN KEY (`aggregate_match_id`)
    REFERENCES `swp`.`match` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
Thanks for any help.

CREATE TABLE IF NOT EXISTS `swp`.`goal` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `team_id` INT NOT NULL COMMENT 'Team that has scored.',
  `player_marker_id` INT NOT NULL,
  `player_assist_id` INT NULL,
  `match_id` INT NOT NULL,
  `minute` VARCHAR(45) NOT NULL,
  `result` VARCHAR(45) NULL,
  `type` INT NOT NULL COMMENT 'All the status that a goal can assume.',
  INDEX `player_marker_id_idx` (`player_marker_id` ASC),
  INDEX `player_assist_id_idx` (`player_assist_id` ASC),
  INDEX `match_id_idx` (`match_id` ASC),
  PRIMARY KEY (`id`),
  INDEX `FK_goal_type_goal_goal_type_id_idx` (`type` ASC),
  INDEX `FK_team_goal_team_id_idx` (`team_id` ASC),
  CONSTRAINT `FK_player_goal_player_marker_id`
    FOREIGN KEY (`player_marker_id`)
    REFERENCES `swp`.`player` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_player_goal_player_assist_id`
    FOREIGN KEY (`player_assist_id`)
    REFERENCES `swp`.`player` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_match_goal_match_id`
    FOREIGN KEY (`match_id`)
    REFERENCES `swp`.`match` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_goal_type_goal_goal_type_id`
    FOREIGN KEY (`type`)
    REFERENCES `swp`.`goal_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_team_goal_team_id`
    FOREIGN KEY (`team_id`)
    REFERENCES `swp`.`team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

UPDATE

actual query:

$query = "SELECT m.*,
  t.name AS team_name,
  t.id AS team_id,
  l.position AS team_rank,
  COUNT(CASE WHEN g.type = 5 THEN 1 END) AS failed_to_score,
  FROM `match` m
  LEFT JOIN goal g ON m.id = g.match_id
  LEFT JOIN team t ON t.id = :team_id
  LEFT JOIN league_ranking l ON l.team_id = :team_id AND l.round_id = :round_id
  WHERE m.round_id = :round_id
  AND m.status = 5 ";

Upvotes: 0

Views: 54

Answers (1)

Lelio Faieta
Lelio Faieta

Reputation: 6663

SELECT minute, result FROM goal WHERE (CAST(minute AS UNSIGNED)<=45 OR minute like '45%') AND match_id='2564824'

if this is your starting point:

id    | match_id | result | minute 
16092   2564824    1 - 0     15
16093   2564824    2 - 0     43
16094   2564824    3 - 0     63
16095   2564824    4 - 0     78

If this is indeed your expected results then you have to show the input data to get this result

UNTESTED

SELECT m.*,
  t.name AS team_name,
  t.id AS team_id,
  l.position AS team_rank,
  COUNT(CASE WHEN g.type = 5 THEN 1 END) AS failed_to_score,
  g.minute
  FROM `match` m
  LEFT JOIN goal g ON m.id = g.match_id
  LEFT JOIN team t ON t.id = :team_id
  LEFT JOIN league_ranking l ON l.team_id = :team_id AND l.round_id = :round_id
  WHERE m.round_id = :round_id
  AND m.status = 5 "
  AND (CAST(minute AS UNSIGNED)<=45 OR minute like '45%');

This integrated query will stop the analysis of each match to the first half. I'd keep the two queries as separate

Upvotes: 1

Related Questions