php项目统计角球数哪边领先?

wen PHP项目 1

PHP项目统计角球数哪边领先

在PHP项目中统计角球数哪边领先,核心思路是对比主队和客队的角球数量,下面给出一个完整的实现方案。

php项目统计角球数哪边领先?

基础实现

<?php
/**
 * 判断角球数哪边领先
 * @param int $homeCorners 主队角球数
 * @param int $awayCorners 客队角球数
 * @return array 包含领先方和差值信息
 */
function getCornerLeader(int $homeCorners, int $awayCorners): array
{
    $diff = $homeCorners - $awayCorners;
    if ($diff > 0) {
        $leader = 'home';
        $leaderName = '主队';
    } elseif ($diff < 0) {
        $leader = 'away';
        $leaderName = '客队';
    } else {
        $leader = 'draw';
        $leaderName = '平局';
    }
    return [
        'leader'      => $leader,       // home / away / draw
        'leader_name' => $leaderName,
        'home'        => $homeCorners,
        'away'        => $awayCorners,
        'diff'        => abs($diff),    // 领先差距
    ];
}
// 使用示例
$result = getCornerLeader(6, 3);
echo "领先方: {$result['leader_name']}, 差距: {$result['diff']} 个角球";
// 输出: 领先方: 主队, 差距: 3 个角球

从数据库统计数据

假设表结构如下:

CREATE TABLE match_corners (
    id INT PRIMARY KEY AUTO_INCREMENT,
    match_id INT,
    team_type ENUM('home','away'),
    corner_time INT,  -- 角球发生分钟
    created_at TIMESTAMP
);

统计某场比赛:

<?php
function getMatchCornerStats(PDO $pdo, int $matchId): array
{
    $sql = "SELECT team_type, COUNT(*) AS total
            FROM match_corners
            WHERE match_id = :match_id
            GROUP BY team_type";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([':match_id' => $matchId]);
    $stats = ['home' => 0, 'away' => 0];
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
        $stats[$row['team_type']] = (int)$row['total'];
    }
    return getCornerLeader($stats['home'], $stats['away']);
}

实时/滚动统计(按时间范围)

统计前N分钟谁领先(常用于滚球):

<?php
function getCornerLeaderByMinute(PDO $pdo, int $matchId, int $minute): array
{
    $sql = "SELECT team_type, COUNT(*) AS total
            FROM match_corners
            WHERE match_id = :match_id AND corner_time <= :minute
            GROUP BY team_type";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([':match_id' => $matchId, ':minute' => $minute]);
    $stats = ['home' => 0, 'away' => 0];
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
        $stats[$row['team_type']] = (int)$row['total'];
    }
    $leader = getCornerLeader($stats['home'], $stats['away']);
    $leader['minute'] = $minute;
    return $leader;
}

批量查询(避免 N+1)

如果需要统计多场比赛:

<?php
function getBatchCornerLeaders(PDO $pdo, array $matchIds): array
{
    if (empty($matchIds)) return [];
    $placeholders = implode(',', array_fill(0, count($matchIds), '?'));
    $sql = "SELECT match_id, team_type, COUNT(*) AS total
            FROM match_corners
            WHERE match_id IN ($placeholders)
            GROUP BY match_id, team_type";
    $stmt = $pdo->prepare($sql);
    $stmt->execute($matchIds);
    $raw = [];
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
        $raw[$row['match_id']][$row['team_type']] = (int)$row['total'];
    }
    $result = [];
    foreach ($matchIds as $id) {
        $home = $raw[$id]['home'] ?? 0;
        $away = $raw[$id]['away'] ?? 0;
        $result[$id] = getCornerLeader($home, $away);
    }
    return $result;
}

关键注意点

  1. 数据来源:角球既可以来自实时事件表(逐次记录),也可以直接用比赛结果表里的汇总字段(home_corners、away_corners),后者查询更快但无法按时间切片。

  2. 角球归属:注意区分"谁开出的角球"(记录方)和"角球归属哪队统计"(有些数据源以对方防守方记录),要统一口径。

  3. 加时/补时:若按分钟过滤,需明确 corner_time 是否包含补时(如 90+3 用 93 或 90 表示)。

  4. 索引优化:match_id 和 match_id + corner_time 上建索引,或对 (match_id, team_type) 建复合索引加速 GROUP BY。

  5. 缓存:直播场景角球更新频繁但查询超多,建议用 Redis 缓存 match_id => [home, away],事件到达时 INCR 对应计数。

如果需要特定的数据表结构、框架(Laravel/ThinkPHP)写法,或 WebSocket 实时推送方案,可以告诉我你的具体场景。

友情链接

欧易欧易欧易欧易欧易欧易欧易欧易直播帮篮球控NBA帮比赛虎聊足球

上一篇php项目认为无欲无求时状态会下滑吗?

下一篇当前分类已是最新一篇

抱歉,评论功能暂时关闭!

友情链接攻略窝 · 礼包窝 · 配置窝 · 换算窝 · 算法窝 · 查文窝 · 手册窝 · 问答窝 · 全文搜 · 手册库
相关站点礼包窝 · 查文窝