简体   繁体   English

SQL PHP在同一查询中选择具有多个条件的列

[英]SQL PHP Select columns with several conditions in same query

I can't figure out how to make a selection of the matches between 2 teams even if is at home or away. 我不知道如何选择两支球队之间的比赛,即使是在家或不在场也是如此。

Example: 例:

I have this table: 我有这张桌子:

MatchID  |  status |  date   |   short (home) |  opponent (Away)
1           ENDED      XXX       TEAM A          TEAM B
2           ENDED      XXX       TEAM B          TEAM A
3           ENDED      XXX       TEAM C          TEAM B
4           ENDED      XXX       TEAM D          TEAM A

I have a lot of matches and I want to make a module where I can show all previous matches between team A and team B. (even if is at home or away). 我有很多比赛,我想制作一个模块,在那里我可以显示队A和队B之间的所有先前比赛(即使是在家或不在场)。

Right now this is my code, but is only showing 1 match of 2 possible matches. 现在,这是我的代码,但仅显示2个可能的匹配中的1个匹配。

$ergebnis = safe_query("SELECT * FROM table WHERE status='ENDED' AND ((opponent = '".$opp_match."' AND short = '".$short_match."') OR (opponent = '".$short_match."' AND short = '".$opp_match."')) ORDER BY date LIMIT 0,5"); 

And I want to limit 0,5. 我想限制为0.5。 Just want the last 5 matches between the 2 teams. 只需要两支球队之间的最后5场比赛。

"opp_match" and "short_match" are connections to the other module. “ opp_match”和“ short_match”是与另一个模块的连接。 When I'm check the match I can check home team and away team with those. 当我检查比赛时,可以检查那些主队和客队。

With or without the limits I can't show more than one result. 有或没有限制,我最多只能显示一个结果。

I just want to show matchID 1 and 2. But right now I'm just getting matchID 1. 我只想显示matchID 1和2。但是现在我只得到matchID 1。

EDIT: I tried another way but I'm loading all ended matches. 编辑:我尝试了另一种方法,但我正在加载所有结束的比赛。

   $ergebnis = safe_query("SELECT * FROM ".PREFIX."upcoming WHERE status='ENDED'"); 
   $i=1;
   while($ds=mysql_fetch_array($ergebnis)) {

   if($ds['short']==$short_match AND $ds['opponent']==$opp_match) {
            eval ("\$matches = \"".gettemplate("matches")."\";");
            echo $matches;
    }
    elseif($ds['opponent']==$short_match AND $ds['short']==$opp_match) {
        eval ("\$matches = \"".gettemplate("matches")."\";");
        echo $matches;
    }
    else echo ''; 

$i++; $ i ++; } }

Will this work? 这样行吗?

SELECT *
FROM table
WHERE status = "ENDED"
AND 
(
    (opponent = "team1" AND short = "team2")
    OR
    (opponent = "team2" AND short = "team1")
)
ORDER BY MatchID DESC
LIMIT 5;

This assumes that if one match has a smaller ID than another, it has been played before the other. 这假定如果一个比赛的ID小于另一个比赛的ID,则该比赛已在另一个比赛之前进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM