繁体   English   中英

SELECT其中column1 = column2

[英]SELECT where column1 = column2

在过去的几个小时里,我一直在努力实现以下目标:

$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是主队,而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]。 这可能吗?

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 

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

听起来好像要查找匹配的行。

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

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

如果有多个具有相同(家,客场)的行,则将获得多个匹配项。 例如,使用:

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

您总共将获得十二行。 (ID值为2和11的行将分别与ID值为3、5和7的行“匹配”。)


UPDATE

消除重复项取决于重复项的来源。 添加DISTINCT关键字将确保结果集中没有任何两行完全相同,但是我怀疑您的重复问题比那更深。。。在多次联赛中, bearstigers在本国和tigers面对对方。

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

仅显示了各列,即可使用GROUP BY和诸如MAX()类的聚合函数为每个“匹配项”获得一个不同的行。

例如:

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

需要注意的是返回home ,并away从T2是多余的,因为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

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

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

WHERE t1.match_id < t2.match_id

如果获得的两行中的哪一条都不重要,则小于还是大于比较都无关紧要。 选择要比较的t1和t2列中的哪一个(“ id”,“ home”或“ away”)都没有关系,所需要做的只是保证t1和t2之间的列比较不同(所以您只会得到镜子的一侧。)

暂无
暂无

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

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