如何在连接条件下选择特定的值?

iszxjhcz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(320)

我有一个包含游戏细节的表,这个表叫做 match ,每个匹配可以有n个目标,这些目标存储在表中 goal .
假设我有以下情况 match :

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

所以这个记录意味着 match 对于主队来说是4比0结束的,我想知道的是:我怎样才能得到唯一的机会 goals 第一次得分?在这种情况下,第一次有两个进球,并且具有以下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

我只需要拿到 minute 每个目标的字段,如何设置查询?
架构

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;

更新
实际查询:

$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 ";
eh57zj3b

eh57zj3b1#

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

如果这是你的出发点:

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

如果这确实是您预期的结果,那么您必须显示输入数据才能获得此结果
未经测试

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%');

这个综合查询将停止对上半场的每一场比赛的分析。我会把这两个问题分开

相关问题