微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 查询多个表 – 根据团队ID显示团队名称

我的桌子结构是

`TblTeam` (`TeamID`, `TeamName`) VALUES
(1,'India'),
(2,'Pakistan'),
(3,'Brazil')
(4,'Poland');

`TblMatch` (`MatchID`, `MatchDate`, `MatchStart`, `MatchEnd`, `Team1ID`, `Team2ID`) VALUES
(1, '19-11-2014', '12:00:00', '13:00:00', 1, 2),
(2, '19-11-2014', '13:10:00', '14:10:00', 4, 3),
(3, '19-11-2014', '14:20:00', '15:20:00', 1, 3),
(4, '19-11-2014', '15:30:00', '16:30:00', 4, 2),
(5, '20-11-2014', '10:00:00', '11:00:00', 1, 4),
(6, '20-11-2014', '11:10:00', '12:10:00', 3, 4);

插入Tblscore(scoreID,TeamID,MatchID,score)值
(1,1,1,5),
(2,2,1,6),
(3,4,2,15),
(4,3,2,26);

我想显示团队名称(Team1ID,Team2ID)说19-11-2014有4场比赛

in PHP the output should be

Time                   : Between :
12:00:00 - 13:00:00      India v/s Pakistan
13:10:00 - 14:10:00      Poland v/s Brazil

SELECT m.MatchID, m.MatchDate, m.MatchStart, m.MatchEnd, m.Team1ID, m.Team2ID, 
t.TeamID, t.TeamName 
FROM TblMatch m, TblTeam t WHERE m.MatchDate ='$todayis' ORDER BY m.MatchDate

PHP

while($row=MysqLi_fetch_array($res)){
    $mid= $row['MatchID'];
    $mdd = $row['MatchDate'];
    $t1 = $row['Team1ID'];
    $t2 = $row['Team2ID'];
    $t1n = $row['TeamName']; 


    echo $t1n . " v/s  . " $t1n ;



 }

score query does not work 

 $query="SELECT 
 m.MatchID, 
 m.MatchDate, 
 m.Team1ID, 
 m.Team2ID, 
 s.TeamID,
 s.MatchID,
 T1.TeamName as TeamName1, 
 T2.TeamName as TeamName2,
 T1S.score as Team1score,
 T2S.score as Team2score
 FROM TblMatch m  JOIN TblTeam T1  ON m.Team1ID = T1.TeamID JOIN TblTeam T2  ON m.Team2ID =      T2.TeamID  JOIN Tblscore s ON m.Team1ID = T1S.TeamID JOIN Tblscore s ON m.Team1ID = T1S.TeamID  JOIN   Tblscore s ON m.Team2ID = T2S.TeamID WHERE s.MatchID=$mid
 ";

解决方法:

您可以将您的匹配表结果与团队表一起加入两次,以便提取匹配信息和每个团队的名称.之后,您只需要在PHP中连接从数据库获取的数据.

SELECT 
    m.MatchID, 
    m.MatchDate, 
    m.MatchStart, 
    m.MatchEnd, 
    m.Team1ID, 
    m.Team2ID, 
    T1.Teamname as Teamname1, 
    T2.TeamName as Teamname2
FROM TblMatch M
JOIN TblTeam T1
    ON M.TEAM1ID = T1.TeamID
JOIN TblTeam T2
    ON M.TEAM2ID = T2.TeamID

PHP代码

while($row=MysqLi_fetch_array($res)){
    $mid= $row['MatchID'];
    $mdd = $row['MatchDate'];
    $t1 = $row['Team1ID'];
    $t2 = $row['Team2ID'];
    $t1n = $row['TeamName1']; 
    $t2n = $row['TeamName2']; 

    echo $t1n . " v/s  . " $t2n ;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐