-1

我需要选择联盟中得分最高的球队。这是比赛栏,它确实取决于 m_id 如果你是 team1_id 或 team2_id 与分数相同,因为联赛中也有复赛。

 CREATE TABLE IF NOT EXISTS `xeg8u_bl_match` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `m_id` int(11) NOT NULL DEFAULT '0',
  `team1_id` int(11) NOT NULL DEFAULT '0',
  `team2_id` int(11) NOT NULL DEFAULT '0',
  `score1` int(11) NOT NULL DEFAULT '0',
  `score2` int(11) NOT NULL DEFAULT '0',
  `match_descr` text NOT NULL,
  `published` char(1) NOT NULL DEFAULT '0',
  `is_extra` char(1) NOT NULL DEFAULT '0',
  `m_played` char(1) NOT NULL DEFAULT '0',
  `m_date` date NOT NULL DEFAULT '0000-00-00',
  `m_time` varchar(10) NOT NULL DEFAULT '',
  `m_location` varchar(255) NOT NULL,
  `bonus1` decimal(10,2) NOT NULL DEFAULT '0.00',
  `bonus2` decimal(10,2) NOT NULL DEFAULT '0.00',
  `team1_strzaly` varchar(255) NOT NULL,
  `team1_strzalyc` varchar(255) NOT NULL,
  `team1_posiadanie` varchar(255) NOT NULL,
  `team1_przejecia` varchar(255) NOT NULL,
  `team1_faule` varchar(255) NOT NULL,
  `team1_zkartki` varchar(255) NOT NULL,
  `team1_ckartki` varchar(255) NOT NULL,
  `team1_kontuzje` varchar(255) NOT NULL,
  `team1_spalone` varchar(255) NOT NULL,
  `team1_rrozne` varchar(255) NOT NULL,
  `team1_celnosc` varchar(255) NOT NULL,
  `team1_dpodan` varchar(255) NOT NULL,
  `team2_strzaly` varchar(255) NOT NULL,
  `team2_strzalyc` varchar(255) NOT NULL,
  `team2_posiadanie` varchar(255) NOT NULL,
  `team2_przejecia` varchar(255) NOT NULL,
  `team2_faule` varchar(255) NOT NULL,
  `team2_zkartki` varchar(255) NOT NULL,
  `team2_ckartki` varchar(255) NOT NULL,
  `team2_kontuzje` varchar(255) NOT NULL,
  `team2_spalone` varchar(255) NOT NULL,
  `team2_rrozne` varchar(255) NOT NULL,
  `team2_celnosc` varchar(255) NOT NULL,
  `team2_dpodan` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=399 ;

现在我正在考虑在 SELECT、MAX 和 SUM 中使用两个函数,我试图做到这一点,但我不知道如何让它发挥作用,我知道如何显示指定球队的主客场进球数,但我不知道知道如何检查哪支球队是最佳射手。有任何想法吗?

好的,在所有评论之后,我决定尝试一下。所以我解决了它,它确实有效,但是我的问题是是否有任何方法可以更容易地做到这一点,是否需要以不同的方式做到这一点?

    //season_id
$link = ConnectToMySql();
$wynik = mysqli_query($link, "SELECT season_id FROM xeg8u_bl_season_teams WHERE team_id = '$teamekid' LIMIT 1");
$m_id = mysqli_fetch_array($wynik);
$r4 = $m_id[0];

//sprawdzam jakie id dni meczowych znajdują się w sezonie o id x
$link = ConnectToMySql();
$wynik = mysqli_query($link, "SELECT MIN(id) FROM xeg8u_bl_matchday WHERE s_id = '$r4'");
$m_id = mysqli_fetch_array($wynik);
$min_r1 = $m_id[0];

$link = ConnectToMySql();
$wynik = mysqli_query($link, "SELECT MAX(id) FROM xeg8u_bl_matchday WHERE s_id = '$r4'");
$m_id = mysqli_fetch_array($wynik);
$max_r1 = $m_id[0];

$link = ConnectToMySql();
$wynik = mysqli_query($link, "SELECT m_id FROM xeg8u_bl_match WHERE m_id LIKE '$r1'");
$id = mysqli_fetch_array($wynik);
$r2 = $id[0];



$link = ConnectToMySql();
$wynik = mysqli_query($link, "Select team, sum(score) as totalscore from (select team1_id as team, sum(score1) as score from xeg8u_bl_match WHERE m_id BETWEEN '$min_r1' AND '$max_r1' group by team1_id union all select team2_id as team, sum(score2) as score from xeg8u_bl_match WHERE m_id BETWEEN '$min_r1' AND '$max_r1' group by team2_id) as scores group by team order by totalscore asc limit 1");
$krol = mysqli_fetch_array($wynik);
$r3 = $krol[0];

像这样离开它可以吗,还是有一种更简单,更好看的方法来做到这一点?正如我所说的那样,它现在确实有效:) 谢谢你们。

4

1 回答 1

2

我想你需要这个:

select team, sum(score) as totalscore
from
(
    select team1_id as team, sum(score1) as score
    from xeg8u_bl_match
    group by team1_id
    union all
    select team2_id as team, sum(score2) as score
    from xeg8u_bl_match
    group by team2_id
) as scores
group by team
order by 2 desc
limit 1
于 2014-08-22T23:52:51.120 回答