如何选择加入条件的特定值?

时间:2018-06-22 14:01:07

标签: mysql sql

我有一个包含游戏详细信息的表,该表称为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得分?在这种情况下,首次有2个进球,并且具有以下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字段,如何设置查询?

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;

更新

实际查询:

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

1 个答案:

答案 0 :(得分:1)

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

此集成查询将停止对上半场每个匹配项的分析。我会将两个查询分开保存