SELECT其中column1 = column2

时间:2014-04-29 17:43:05

标签: php mysql sql

过去几个小时我一直在努力完成以下任务,但没有任何运气:

$stmt = $db->query( "SELECT league_match_id as match_id, league_match_home_team as home_team, league_match_away_team as away_team FROM $table WHERE (( home_team = away_team ) AND (away_team = home_team))" );

假设我们有team1和team2。在Team1回家并且Team2离开的情况下进行比赛。另一个匹配(行)存储在team2为home并且team1离开的位置。我想用一个查询选择两个团队。

没有团队在玩自己,我想获得2行,其中home_team和away_team的值被镜像。

有人可以帮助我走上正轨吗?

*更新*

我得到的回报如下:

Array
(
   [0] => Array
    (
        [t1_id] => 26
        [t1_home] => 2
        [t1_away] => 1
        [t2_id] => 24
        [t2_home] => 1
        [t2_away] => 2
    )

   [1] => Array
    (
        [t1_id] => 28
        [t1_home] => 3
        [t1_away] => 1
        [t2_id] => 25
        [t2_home] => 1
        [t2_away] => 3
    )

   [2] => Array
    (
        [t1_id] => 24
        [t1_home] => 1
        [t1_away] => 2
        [t2_id] => 26
        [t2_home] => 2
        [t2_away] => 1
    )

   [3] => Array
    (
        [t1_id] => 29
        [t1_home] => 3
        [t1_away] => 2
        [t2_id] => 27
        [t2_home] => 2
        [t2_away] => 3
    )

   [4] => Array
    (
        [t1_id] => 25
        [t1_home] => 1
        [t1_away] => 3
        [t2_id] => 28
        [t2_home] => 3
        [t2_away] => 1
    )

   [5] => Array
    (
        [t1_id] => 27
        [t1_home] => 2
        [t1_away] => 3
        [t2_id] => 29
        [t2_home] => 3
        [t2_away] => 2
    )

)

当Array [0]和Array [2]相同时,它们只是镜像的。我可以在这里摆脱重复吗?我希望只有Array [0]或Array [2]。这可能吗?

2 个答案:

答案 0 :(得分:2)

SELECT 
   t1.league_match_id , 
   t1.league_match_home_team , 
   t1.league_match_away_team ,
   t2.league_match_id , 
   t2.league_match_home_team, 
   t2.league_match_away_team   
FROM 
   {$table} t1 JOIN {$table} t2 ON t1.league_match_home_team=t2.league_match_away_team as away_team  AND 
                                   t2.league_match_home_team=t1.league_match_away_team as away_team
 GROUP BY
   t1.league_match_id , 
   t1.league_match_home_team , 
   t1.league_match_away_team ,
   t2.league_match_id , 
   t2.league_match_home_team, 
   t2.league_match_away_team 

答案 1 :(得分:2)

我怀疑“主队”与“客队”是否相同会有任何行。

听起来好像你想找到匹配的两个行。

根据您查询中的条件,听起来您可能想要这样的内容:

SELECT t1.league_match_id         AS t1_match_id
     , t1.league_match_home_team  AS t1_home_team
     , t1.league_match_away_team  AS t1_away_team
     , t2.league_match_id         AS t2_match_id
     , t2.league_match_home_team  AS t2_home_team
     , t2.league_match_away_team  AS t2_away_team
  FROM $table t1
  JOIN $table t2
    ON t1.league_match_home_team = t2.league_match_away_team
   AND t1.league_match_away_team = t2.league_match_home_team

这假设您在表中有相应的行,例如

 id  home   away
 --  -----  ------
  2  bears  tigers
  3  tigers bears

如果有多个行具有相同的(home,away),您将获得多个匹配。例如,使用:

 id  home   away
 --  -----  ------
  2  bears  tigers
  3  tigers bears
  5  tigers bears
  7  tigers bears
 11  bears  tigers

你总共得到12行。 (id值为2和11的行将分别与id值为3,5和7的行“匹配”。)


<强>更新

删除重复项取决于重复的来源。添加DISTINCT关键字将确保结果集中没有两行完全相同,但我怀疑您的重复项问题比那些bearstigers更深在多场联赛中,主场和客场都面对面。

在这种情况下,您需要在表中添加一些内容,并使用一些谓词来限制匹配。这可能是日期,也是获取“最新日期”的一些方法,但这取决于表格中的其他内容。

只显示了列,GROUP BYMAX()等聚合函数可用于为每个“匹配”获取一个不同的行。

例如:

SELECT MAX(t1.league_match_id)    AS t1_match_id
     , t1.league_match_home_team  AS t1_home_team
     , t1.league_match_away_team  AS t1_away_team
     , MAX(t2.league_match_id)    AS t2_match_id
     , t1.league_match_away_team  AS t2_home_team
     , t1.league_match_home_team  AS t2_away_team
  FROM $table t1
  JOIN $table t2
    ON t1.league_match_home_team = t2.league_match_away_team
   AND t1.league_match_away_team = t2.league_match_home_team
 GROUP 
    BY t1.league_match_home_team
     , t1.league_match_away_team

请注意,从{t}返回homeaway是多余的,因为t1.home = t2.away等。t1t2的值相同,但homeaway被交换。

要限制“反向”行,所以你得到(bears,tigers)而不是(tigers,bears),你可以指定一个额外的谓词,这样你只得到一个反面的“一边”:

AND t1.league_match_home_team < t2.league_match_home_team

<强>后续

(我的查询中有一个拼写错误,第一个JOIN谓词应该在右侧指定t2.。我相信OP发现了这个问题并修复了它。)

根据最新的更新,为了消除结果集中的“镜像”反向行,您可以添加这样的谓词(如果您的查询有一个,则在GROUP BY子句后面。)

  HAVING t1_id < t2_id

(与HAVING子句不同,(WHERE子句可以引用分配给返回列的别名。)

如果查询中没有GROUP BY,那么使用WHERE子句可能会获得更好的性能:

WHERE t1.match_id < t2.match_id

如果你得到的两行中的哪一行无关紧要,那么它是否小于或大于比较并不重要。您选择比较哪个t1和t2列(“id”,“home”或“away”)并不重要,所需要的只是t1和t2之间保证不同的列的比较(所以你只能得到镜子的一面。)